Let's say we have a notification service which read an event from message queue and notify all web clients in real time. I know how web socket work but i am puzzled when there is an API gateway in between then how web socket connection is maintained between client, API gateway and notification service.
Please help! Thanks
Edit: Architecture:
The WebSocket API is an advanced technology that makes it possible to open a two-way interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.
Another advantage of this in a world full of web frameworks is that the web framework you already use for your rest api calls can add support for this protocol and without any changes on server-side, you can represent rest endpoints over websocket.
Fast Reaction TimeWebSockets allow for a higher amount of efficiency compared to REST because they do not require the HTTP request/response overhead for each message sent and received.
WebSocket approach is ideal for real-time scalable application, whereas REST is better suited for the scenario with lots of getting request. WebSocket is a stateful protocol, whereas REST is based on stateless protocol, i.e. the client does not need to know about the server and the same hold true for the server.
A websocket connection opened by a client must eventually connect to a websocket server.
The job of the API gateway is to accept an incoming websocket connection from a client and correctly route it to a websocket server. An API gateway will redirect ALL data sent from a client websocket to the correct back-end service and will maintain the connection the entire time.
The root of your question is "how can I have a client with a websocket connection receive a live update from the notification service?". The simplest answer would be to start a websocket server on the Notification Service, let each client connect to the API gateway, then have the API gateway route that traffic to the Notification Service.
If you have further requirements by the clients to transform the data coming out of the Notification Service, then you could:
You should not mix the concepts. An API Gateway is hiding your infrastructure from your client. It can be a single frontend for many services, in the "Backends for frontends" sense. It can also be responsible for many other things, such as authentication.
A web socket server can sit in parallel to your API Gateway. Another domain or another port. Let's say you use a web socket server like http://nchan.io. The events from your application go through your message broker or whatever messaging integration pattern you use. A consumer can pick up this events and publish them through the Nchan server. Clients (for example Browsers) connect to the Nchan server and will be informed about the events.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With