So I've read some articles about scaling Socket.IO. For various reasons I don't want to use built-in Socket.IO scaling mechanism (mostly it seems to be inefficient, since it publishes a lot more stuff to Redis then required from my point of view).
So I've came up with this simple idea:
Each Socket.IO server creates Redis pub/sub/store clients, connects to Redis and subscribes to a channel. Now, when I want to broadcast data I just publish it to Redis and all other Socket.IO servers get it and push it to users.
There is a problem, though (which I think is also a problem for Socket.IO built-in mechanism). Let's say I want to know the number of all connected users. There are at least two ways of doing that:
Server A publishes
give_me_clients
to Redis. Then each Socket.IO server counts connections and publishesnumber_of_clients
. Server A grabs this data, combines it and sends it to the client.Each server updates
number_of_clients_for::ID_HERE
in Redis whenever user connects/disconnects to the server. Then Server A just fetches data and combines it. Might be more efficient.
There are problems with these solutions though:
Server A is not aware of other servers. Therefore he does not know when he should stop listening to
number_of_clients
. One could fix it with making Server A aware of other servers: whenever a server connects to Redis he publishesnew_server
(Server A grabs the data and stores it in memory). But what to do, when Redis - Socket.IO connection breaks? Is there a way for Redis to notify clients that one of the client disconnected?Actually the same as above. When a Socket.IO server crashes how to clear
number_of_clients
data?
So the real question is: can Redis notify (publish to chanel) clients that the connection with one of them has just ended??