server-sent-events
server-sent-events copied to clipboard
Saving SSE Emitters Issue
A doubt on this.. How we need to implement this on a production based application having multiple instance running. how can we save this SSE emitter to database/any distributed ds instead of concurrent map. Is it possible ?
You're right this service isn't scalable and for the system I was working with that wasn't an issue because we didn't have demanding workload. I don't believe we can store object reference in any distributed DS.
Some ways to solve it I could think of would be:
- implement routing between the instances so that if the given instance does not have required emitter it forwards the request to other instances,
- store in the distributed DS some unique ID which would identify the instance and use it to route the request to the instance with the emitter,
But of course that's a lot of trouble so probably for production system which requires scaling I wouldn't go with SSE but instead use e.g. API Gateway with WebSockets communication.
Still for some solutions SSE might be the simple and good enough way.
Actually I though of a scalable solution. We could place a queue in front of the notification service. We would push all the notifications to the queue and the instance of the service having the connection corresponding to the notification in the queue would pull the notification and deliver it to the client.
This would require some additional handling of messages hanging in the queue etc. but would allow us to use SSE for our notifications and wouldn't add to much complexity.
Thanks for the advices. I'm going to try your queue idea.