Websockets in microservices architecture

Websockets

A websocket connection opened by a client must eventually connect to a websocket server.

API Gateway

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.

How everything works together…

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.

  • Client <=> API Gateway <=> Notification Service

Taking it further…

If you have further requirements by the clients to transform the data coming out of the Notification Service, then you could:

  1. Stuff that business logic into the Notification Service (not recommended).
  2. Or, add another service with the transformation logic between the API gateway and the Notification Service which is called the Backends for Frontends microservice design pattern (recommended):
    • Client <=> API Gateway <=> Notification Server (transformation logic) <=> Notification Service.
  3. Or, if your API gateway of choice is designed to hold business logic and transform data; put the transformation logic directly in the API gateway.

Leave a Comment

tech