question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to delete disconnected sockets???

See original GitHub issue

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 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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
marcelodieguescommented, Jan 10, 2017

@darrachequesne Can you help me based in my code?

1reaction
darrachequesnecommented, Jan 10, 2017

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 + ').');
    });
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Remove objects on disconnect socket.io - Stack Overflow
If you don't cleanup, then yes, they will stay there forever since I assume you are making them global. You should cleanup once...
Read more >
How to remove disconnected sockets - MSDN - Microsoft
1. Client device make a connection to the server. 2. Sending data every 10 seconds without disconnection. 3. Client device is not concerned ......
Read more >
removing a socket from list on disconnect - SFML
removing a socket from list on disconnect. ... if (client.receive(packet) == sf::Socket::Disconnected) {
Read more >
Rooms | Socket.IO
Upon disconnection, sockets leave all the channels they were part of automatically, and no special teardown is needed on your part. You can ......
Read more >
Deleting clientsocket in slot for signal disconnected ... - Qt Forum
Initially I was deleting the memory for client socket in SlotForDisconnected() using delete <clientSocket_Object> and setting it to NULL ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found