1

I wrote this code basically:

socket.on('disconnect', function (socket) {
    var remove_id = usersonline.indexOf(socket.user);
    usersonline.splice(remove_id, 1);
    socket.broadcast.emit('usergone', {
        'left_user' : socket.username
    });
});

that's server side, here is client side:

socket.on('usergone', function(data){
    var old_user = data['left_user'];
    console.log(old_user);
    $(old_user).remove();
});

It doesn't go off however, I tested it doesn't execute client side code which I guess means it doesn't get to it.

Any ideas?

Basically I am building chat application. So far it works fine, as user joins they get added to users online list etc... but when user leaves - I cannot find way to tell all other online users that user has left and remove him from users online list/tab.

I am creating socket connection with following:

var http = require('http'),

    express = require('express'),

    app = express(),

    jade = require('jade'),

    io = require('socket.io'),

    server = http.createServer(app);



io = io.listen(server);


server.listen(3000);



app.set('views', __dirname + '/views');

app.set('view engine', 'jade');

app.set("view options", { layout: false });

app.use(express.static(__dirname + '/public'));

app.get('/', function(req, res){

    res.render('home.jade');

});


var usersonline = [];

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

functions here.

});

When user connects: their username is added to usersonline, then it broadcasts to my script.js (which is client side). It parses the array of usersonline and loads list of users in chatroom for client that just joined. It also sends just joined users username and tells others that he is there now, and he is added to list of online users.

    socket.username = data;
    usersonline.push(data);
    socket.emit('loadusers', usersonline);
    socket.broadcast.emit('newuser', {
        'username' : data
    });

All works fine. Except I cannot come up with way to notify all other users when user has left. I thought I can easily: on disconnect - broadcast to all others that [username] has left. My users online list works by appending the current list and adding new user.

socket.on('newuser', function(data){
$('#chatUsers').append('<div class="user" id="'+data['username']+'">'+data['username']+'</div');

});

so when user leaves I thought I can easily just remove that div and user will dissapear from list of online users dynamically. But this doesn't work. Need an alternative way.

arleitiss
  • 1,304
  • 1
  • 14
  • 38

1 Answers1

5

There's an error in your code: you're passing the disconnect callback an unnecessary socket parameter

socket.on('disconnect', function (/* socket <- remove this */) {
    var remove_id = usersonline.indexOf(socket.user);
    usersonline.splice(remove_id, 1);
    socket.broadcast.emit('usergone', {
        'left_user' : socket.username
    });
});

And actually I was wrong about the socket.broadcast not working, it does work! You just need to correct the above error.

laggingreflex
  • 32,948
  • 35
  • 141
  • 196
  • So wait, you are saying that I should parse disconnected users ID from client side rather than server side? – arleitiss Oct 17 '14 at 11:46
  • Doesn't seem to work with client side disconnect event either. I need to find way to tell all users that specific username has left the chat. – arleitiss Oct 17 '14 at 15:23
  • @arleitiss Try `io.emit` instead of `broadcast` – laggingreflex Oct 17 '14 at 16:26
  • That didn't work either. Any alternatives/work arounds that would let me tell all connected users which user has left? – arleitiss Oct 17 '14 at 16:42
  • @arleitiss Just to understand better what you're doing, You have 2 clients and when one of them disconnects you want to tell the other, right? How are you opening these two clients? For your question, [see this](http://stackoverflow.com/questions/6563885/socket-io-how-do-i-get-a-list-of-connected-sockets-clients) – laggingreflex Oct 17 '14 at 16:48
  • I've seen many of those answers last night, however it seems that since 2011 the sockets.io have updated, and most of it's methods are depreciated. I will update my question for more details. Give me a min. – arleitiss Oct 17 '14 at 16:52
  • @arleitiss If you change it to `io.emit` it oughta work.. How are you disconnecting a user? closing the tab? – laggingreflex Oct 17 '14 at 17:05
  • @arleitiss There's an error in your code: you're passing the disconnect callback an unnecessary `socket` parameter. Check updated answer – laggingreflex Oct 17 '14 at 17:29
  • Yeah I had that part removed but it still doesn't work. Basically: server side on disconnect emits 'usergone' client side: on usergone = console.log but it doesn't log anything. I tried both io.emit and socket.broadcast.emit No response whatsoever. Any other way I can try and test if it goes through? maybe there is some kind of problem with console.log() – arleitiss Oct 17 '14 at 17:34
  • I suppose the only way then is to make client side timer that will send request to server to get new list of usersonline array and then decide which user went off and which are still present. – arleitiss Oct 17 '14 at 20:18
  • You should make an empty application just to test this. And if that works (surely must) then copy paste your app's functionality to the empty app little-by-little to find exactly the code that is causing it to not work in the expected manner – laggingreflex Oct 18 '14 at 02:19
  • I managed to do workaround, every 5 seconds the server broadcasts the latest list of users currently in room, it compares latest to the ones that are written as online, if any of them exists in 1 array but not in second - it removes them. Works perfect, though wish it wasn't a workaround – arleitiss Oct 18 '14 at 03:02