Manu Manu - 1 year ago 263
Javascript Question

io.emit vs socket.emit

I'm new to and have run in to something that seems pretty weird. I don't actually know the difference between

but I can't find an explanation anywhere.

io.on('connection', function(socket){
io.emit('connected') // <<<< HERE >> socket.emit('connected');
socket.on('disconnect', function(){
socket.on('chat message', function(msg){
io.emit('chat message', msg);


That's my server stuff however when I change the
that message only gets displayed when the user who is connecting connects.
sends the message to all users.

Maybe it's supposed to be like that or maybe its just some horrible hack? Let me know if you need the client side HTML.

Answer Source

The io variable represents the group of sockets. The code you have starts on line one with providing a function in the second parameter that gives you a socket variable every time a new connection is made. The socket variable is only for communicating with each individual connection. You may not see it in the code but there will be one socket variable for each connection established

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download