Socket.io: How to delete disconnected sockets???

Created on 19 Nov 2016  路  5Comments  路  Source: socketio/socket.io

I use socket.io version 1.5.1 and I want to delete disconnected sockets, but I don't find an answer to fix this.

This is the socket server in node
```var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var redis = require('redis').createClient();

redis.subscribe('change-financial-flag');
io.on('connection', function (socket){

console.log("connected user: " + socket.id);
clients.push(socket);

redis.on('message', function(channel, message) {
    socket.emit(channel, message);
    console.log('emit: ' + channel + ' - socket: ' + socket.id);
});

socket.on('disconnect', function(){
    socket.disconnect(true);
    console.info('disconnected user (id=' + socket.id + ').');
});

});

http.listen(5001, function () {
console.log('listening on *:5001');
});

This is my output:

connected user: Bso5jOM3BmifSNUgAAAA
disconnected user (id=Bso5jOM3BmifSNUgAAAA).
connected user: yt0sLF5Mm5vts1ujAAAB
disconnected user (id=yt0sLF5Mm5vts1ujAAAB).
connected user: nZWM2gwE6pJXcIjsAAAC
disconnected user (id=nZWM2gwE6pJXcIjsAAAC).
connected user: MKhspxnSpmnxG4nAAAAD
disconnected user (id=MKhspxnSpmnxG4nAAAAD).
connected user: FnvUJsLKjtIC80MvAAAE
emit: change-financial-flag - socket: Bso5jOM3BmifSNUgAAAA
emit: change-financial-flag - socket: yt0sLF5Mm5vts1ujAAAB
emit: change-financial-flag - socket: nZWM2gwE6pJXcIjsAAAC
emit: change-financial-flag - socket: MKhspxnSpmnxG4nAAAAD
emit: change-financial-flag - socket: FnvUJsLKjtIC80MvAAAE
```

Most helpful comment

@darrachequesne Can you help me based in my code?

All 5 comments

The problem here is that you keep a reference to the socket in redis.on('message', ... ).

redis.subscribe('change-financial-flag');
redis.on('message', function(channel, message) {
    io.emit(channel, message); // emit to all connected users
    console.log('emit: ' + channel)
});

io.on('connection', function (socket){
    console.log("connected user: " + socket.id);

    socket.on('disconnect', function(){
        // socket.disconnect(true); <- that seems useless
        console.info('disconnected user (id=' + socket.id + ').');
    });
});

@darrachequesne Can you help me based in my code?

@marcelodiegues in your code, you did register a new event listener (redis.on('message', /* */) {})) every time a user connects.

The change I suggest is to have a global listener, and to emit to all connected sockets (the io.emit() above) everytime a message is received on the channel change-financial-flag.

@darrachequesne Can you show a example?

I think it because the socket reference was saved to an array and haven't being removed.

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

    console.log("connected user: " + socket.id);
    clients.push(socket); // <-- This
...

Try splice clients array

And if you want to get the connected users just look at this object
Object.keys(io.sockets.sockets)

Was this page helpful?
0 / 5 - 0 ratings