express.io icon indicating copy to clipboard operation
express.io copied to clipboard

Using 'connect' and 'disconnect' events

Open adamboazbecker opened this issue 11 years ago • 7 comments

I'm unable to use on('connect') or on('disconnect') with express.io.

I read that events like 'connect' and 'disconnect' should not be used with app.io.route or req.io.on unless I know what I'm doing (and I don't).

So in what cases can they be used? I'm interested in recording the number of people in a room (without periodically having to find the lengths of each room) and so would like to record whenever a socket disconnects.

Sincerely appreciate any help!

adamboazbecker avatar Oct 14 '13 21:10 adamboazbecker

Well, at really i'm also don't know why we shouldn't use them, but i'm using... :stuck_out_tongue: the disconnect you can use as this: At the app.js which will be the server you can set this two functions:

// I've omitted the code which creates the server...

app.io.route('disconnect', function(req) {
  console.warn('io.route.disconnect');
  // Your function
  }
})

app.io.route('connected', function(req) {
  console.warn('io.route.disconnect');
  // Your function
  }
})

The disconnect functions will be called automatic when the socket get closed, after this you just need to set the command at the frontend which will call the connect when a new socket get connected.

app.html

<script>
socket: io.connect(),
socket.emit('connected');
</script>

The socket.emit will trigger the connected function that you have setted at the app.js. Hope this can help you...

wallynm avatar Oct 14 '13 22:10 wallynm

After testing a little more, i've had discover a problem using the disconnect event, when you configure your own function, the system will erase your req.session data inside the socket that you tried to run the disconnect.

Trying to find something about this...

wallynm avatar Oct 16 '13 05:10 wallynm

Thanks a lot for the response. Right - because my intention is to record the number of people in a particular room, I would like to know which rooms the socket was connected to, but I can't seem to find this information inside the disconnected socket; it's as if, as you say, the req.session data had been erased.

Do you happen to know of other ways to record from which room a socket disconnected?

Much appreciated!

adamboazbecker avatar Oct 16 '13 17:10 adamboazbecker

@adamboazbecker , you can try to get the rooms debugging the var req.socket inside your route connect... Inside the socket, you have other objects, like the manager and others... Inside one of them, which i don't remember right now (i can check for you) have one called Rooms which store the all the sockets connected, maybe you can use it for count how many are connected.

wallynm avatar Oct 18 '13 15:10 wallynm

Having the same connect event problem. Tried routes for connect, connection and connected with no luck.

nleclerc avatar May 22 '14 15:05 nleclerc

The workaround is to bind the event directly on the sockets instance:

app.io.sockets.on ('connection', function (socket) {
    console.debug('> CONNECTION');
})

A bit lame but works.

nleclerc avatar May 22 '14 15:05 nleclerc

I used connection and disconnect events in my app and it worked with something like this:

app.io.sockets.on('connection', function(socket) {
  // make your connection actions
  myClients.addClientBySocketId(socket.id); // in example, we create a new Client with the socket id
  // and attach the disconnect event
  socket.on('disconnect', function() {
    // make your disconnection actions
    myClients.removeClientBySocketId(socket.id); // remove the client with the socket Id
  }
}

Thanks to @nleclerc for the sample above Hope that helps :)

aitorllj93 avatar Dec 01 '14 14:12 aitorllj93