I have a nodejs application with cluster and I use hazelcast for scalebility. I need to add socket.io for real time messaging but when I connect to the socket on the worker processes, my client’s socket object has lost.
I research for a while but I didn’t find any solutions. There are too many examples with Redis but I cannot use Redis. I must use Hazelcast.
How can I share the sockets to every process and also every server with Hazelcast? Is there a way?
Node version:v10.16.3
Socket.io version: 2.3.0
Thanks in advance.
2
Answers
You can’t share sockets across processes. What you can do is distribute messages across processes using redis or some other system. If you may use the long polling fallback of socket.io you will need to ensure sticky sessions is enabled otherwise you need to ensure you are only using websockets. Socket.io provides an out of the box solution to integrating redis into your system to distribute messages across processes if your use case is simple.
Hazelcast provides you both pub/sub mechanism and in-memory caching on a JVM-based Hazelcast cluster and you can connect to cluster via Hazelcast node.js client. So, It gives an ability to manage and scale Socket.io application via pub/sub topics and storing socket ids in a Hazelcast IMap datastructure.
I can share an example pseudo-code for you:
If you can share a specific code piece, I can edit my answer according to your exact requirements.