Skip to content Skip to sidebar Skip to footer

Socket.io Double Emits When Executing Express (v4) Route

I have a problem with double emits when executing express (v4) route. Lets for example run bellow code. Request (pipe) and mkdir will start immediately (race condition) and there w

Solution 1:

This line of code:

req.io.on('connection', function(socket) {...});

Just adds a new duplicate event handler every time you run this route. So, after running the route twice, you have two event handlers for the connection event. After running that route three times, you have three event handlers for the connection event and so on. They just accumulate and never go away.

So, that explains the duplicate processing. You have duplicate event handlers so the same connection event is processed multiple times.

I can't really suggest a fix because I don't follow what you're trying to accomplish and this whole scheme of trying to catch the next connection event and assuming that belongs to a specific page is flawed (full of race condition issues) - not to mention that it adds a never ending set of duplicate event handlers. You will need one listener for the connection event that is outside any route handler and then use that incoming event to do whatever you want to do.

Post a Comment for "Socket.io Double Emits When Executing Express (v4) Route"