Dirk Dunn
Dirk Dunn

Reputation: 363

Socket.io client event listeners not listening after server restart and reconnect

Socket.io client works great until the server breaks / restarts.

Once the server comes back up, it says that it's reconnected, but all of my client listeners no longer trigger when an event is emitted.

There doesn't seem to be a lot online for this, but from what i've read so far, here's was i've tried:

  1. Wrapping all of my .on listeners in a function, and then recalling that function every time the client library reconnects / connects.

  2. Wrapping all of my io instances in a function that returns a fresh instance of them every time I call it.

  3. Keeping track of all of my rooms on the server, and then rejoining all of them once reconnected.

  4. Set 'multiplex' to false when creating io instance.

None of these things have brought me any luck.

Here is the most relevant github issue, that does not have an answer: https://github.com/socketio/socket.io/issues/1633

It would seem since I have all of the room names on my client, that I would be able to re-connect these without using something like Redis? Thanks.

UPDATE: After a day of trying many different strategies, it seems like creating a .on('connection',fn) listener for each namespace will make the client and server reconnect properly

 io.of('namespace').on('connection', function(){
        console.log("namespace connected")
 });

If you do not have this, socket.io will still work and connect, but it will not reconnect with working client listeners.

I would think this should be something updated in the library.

Anyway, pertaining to my particular problem I am still left with one issue:

I need to be able to handle dynamic namespaces

io.of('namespace-<id>').on('connection', function(){
     console.log("namespace id: <id> connected")
});

It would be perfect if I had wildcard support, something such as

io.of('namespace-*').on('connection', function(){
     console.log("namespace id: <id> connected")
});

Would fit my situation perfectly. I've browsed around at different libraries such as this one:

https://www.npmjs.com/package/socket.io-events

but it only supports wildcard and regex for the events, not the namespaces.

Upvotes: 2

Views: 1367

Answers (1)

Dirk Dunn
Dirk Dunn

Reputation: 363

The answer I came up for this solution was to make the event names dynamic instead of the namespaces.

so instead of

io.of('namespace-<id>').on('myevent', function(){
     console.log("namespace id: <id> connected")
});

I do

io.of('namespace').on('myevent-<id>', function(){
     console.log("namespace id: <id> connected")
});

However, I do want to say that the only reason why I did this is because of how I was architecting my application. I wanted to use namespaces when it came to a separation of concerns (E.G. page to page), and rooms when it came to separation of behavior (E.G. caching notifications for two different parties viewing the same data).

Unless you have a concrete reason for particularly wanting dynamic namespaces, you should use rooms, as this is what they were designed for.

Upvotes: 0

Related Questions