Skip to content Skip to sidebar Skip to footer

Socket.io Connection Keep Increasing Each Time A New Connect Has Been Made

I'm using Express4 with a router that points to path /, and that is handled by a JS file named chat.js. And my IO object is already bind to app.io, so inside my chat.js I'll call m

Solution 1:

Every time your / route is hit, you create a new duplicate io.on('connection', ...) event handler. So, after that route is hit 3 times, you have 3 event handlers for the connection event. So, when it occurs, your code gets called 3 times.

Instead, you should do the io.on('connection', ...) only once outside the route.


FYI, you don't seem to be doing anything useful with the uid you are creating because you don't associate it with any particular connection. FYI, each socket.io connection already has a unique socket.id which is uniquely associated with each socket.io connection so that you can get the id from the socket or can retrieve the socket given only the id.

Post a Comment for "Socket.io Connection Keep Increasing Each Time A New Connect Has Been Made"