Protocol translation in NestJS microservices
·Aug 19, 2021·
1 min read
Making async microservices talk to clients that use "standard" web-friendly API protocols (think mobile and web applications) requires protocol translation. Here is a very simple idea, inspired by nats proxy project, on how to achieve it in NestJS with NATS microservice transport.
The complete source code for this article is available on Github. The README provides the setup and usage instructions as well as main files to look at. It's a Nx Monorepo that contains the following two applications:
- Gateway: This application exposes REST interface to clients, receives HTTP requests, dynamically forms the NATS subject name to send message to based on incoming request verb and path, sends the message to the subject, waits for NATS response from microservice and once received, sends out HTTP response back to client.
- Microservice: This application listens on NATS subject and responds with a string message when invoked.
The overall interaction looks like this:
That's it for today. Happy coding! À bientôt 🙋♂️!