Search code examples
node.jssocket.ioreconnect

Are sockets reused on reconnect event?


A socket is created when a user connects to the server.

The connection is broken because of interwebz malfunction.

Client reconnects automatically.

Will the socket connection be the same as when he first connected? Or will the socket be a new one?

Thanks!


Solution

  • A new connection is established on reconnect. It can be checked by printing the socket.id.

    On server:

    var io = require('socket.io)(server);
    io.on('connection', function (socket) {
      console.log('socket id is ' + socket.id); }
    

    Excerpt from socket.io documentation

    The property is present once the socket has connected, is removed when the socket disconnects and is updated if the socket reconnects.