Question based on this answer: stack overflow
I tried to find this solution, but nothing seems to work the way I need.
Clustering expressjs and socket.io we can exchange sessions using redis and send io messages inside io world ( io.sockets.on('connection', ...). The problem is that we want to send a message (or use a simple socket.join/leave ) inside the expressjs world ( route.get/post ).
If we do not use clusters, we can attack the client socket object for the express request object (or simply the export io object), and then use it at any time on any GET / POST route.
On the other hand, if we are clusters and use the mentioned method to get the socket object inside the expressjs world, sometimes the socket object is undefined because the socket object for this client is initialized in another worker .
Stream example:
- The client connects to http: // localhost and
worker 1 processes this request. - After loading the page, the client connects to
socket.io . Worker 2 processes this connection. - The client performs a POST, and then
worker 1 or worker X processes this request.
In this case, when the client performs POST, only Worker 2 knows the socket object for this client. This way it will get an undefined socket object.
So the question is:
How can we get the client socket object from any worker to reuse it on the expressjs request object.
Maybe my code is wrong, but almost like a link to the answer mentioned above.
Notes
- Do not want to use some kind of proxy.
- Do not want to switch to other libraries (expressio, sockjs ...)
- Sorry for my English:)
Using the latest nodejs, socket.io, expressjs, socket.io-redis, redis ... versions
Feel free to ask something!
UPDATE 1
Possible solution, but still need to test it. I don't know if this is really good: a solution.
- UPDATE 3 : working code on own answer
UPDATE 2
As update 1, but using https://nodejs.org/dist/latest-v5.x/docs/api/cluster.html#cluster_event_message