In socket.io, socket.disconnect() to a single socket, disconnect all socket connection

528 views Asked by At

I have a browser, which connects to server using socket.io with transport as websocket only.

I validate all socket connecting to my server, using simple logic and is working fine.

Update-1

Now the problem occur when Internet fluctuates, browser is creating many websocket connection. (Issue similar to https://github.com/socketio/socket.io/issues/430)

As application requires one connection per browser and thus except one all other socket are being invalidated by validation code. But when I disconnect a invalidated socket all sockets are being disconnecting.

Simplified Code

const soredis = require('socket.io-redis');
const io = require('socket.io')(3000);

io.adapter(soredis({host: localhost, port: 6379}));

io.sockets.on('connection', function(socket) {

  setTimeout(function () {
    getSocket(socket.id, (err, res) => { //get data from redis
      if (err) { //If not found in redis
        socket.disconnect();
        return;
      }
    });
  }, 15000);


  socket.on('register', function(data, cb) {
    if (!data.key) {      
       return cb("Error");
    }
    saveSocket(socket.id); //save to redis
    return cb();  
  });

});

Any possible reason why and how to resolve the same??

0

There are 0 answers