Share Websocket Session between Spring Micro Service

1.8k views Asked by At

I've a Spring boot Application for Web sockets. I'm not using Stomp Web socket. Is there way we can share web socket sessions across multiple instance of micro service.

Is there a way we can save websocket session in Redis or cassandra?

My use case is, i've multiple instance of my micro service is running, which is listening a kafka queue, so when a message received, i need to send it to the client using web socket session.I'm saving the session in the micro service as a MAP. My problem is any one of my micro service is getting the message, if the session is not available with that micro service the message is not going to the client.

If i'm able to save the websocket sesssion in REDIS or Cassandra, i can query the session and sent to the client.

I can't use Stomp web socket as per the requirement, it has to be normal websocket.

1

There are 1 answers

0
Ulises On

You can't. You have to implement some sort of routing from whatever receives the kafka message, to your micro service. One simple way to do it would be to store in any datastore (mongo, redis, etc) the IP of the service instance for a given client. That way when you get the message from kafka, an you know who is it for, you lookup which machine has the websocket session for that client. Then you call some http endpoint on that IP that you implement to relay a message for a session it's handling.