I'm new to socket.io and have run in to something that seems pretty weird. I don't actually know the difference between socket.emit
and io.emit
but I can't find an explanation anywhere.
io.on('connection', function(socket){ io.emit('connected') // <<<< HERE >> socket.emit('connected'); socket.on('disconnect', function(){ io.emit('disconnect') }); socket.on('chat message', function(msg){ io.emit('chat message', msg); }); }); server.listen(3000);
That's my server stuff however when I change the io
to socket
that message only gets displayed when the user who is connecting connects. io.emit
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.
Simply said Each socket emits its msg to a server(io is an instance of server) and server, in turn, emits it to all connected sockets.
We can send the message to all the connected clients, to clients on a namespace and clients in a particular room. To broadcast an event to all the clients, we can use the io. sockets. emit method. Note − This will emit the event to ALL the connected clients (event the socket that might have fired this event).
Key Differences between WebSocket and socket.ioIt provides the Connection over TCP, while Socket.io is a library to abstract the WebSocket connections. WebSocket doesn't have fallback options, while Socket.io supports fallback. WebSocket is the technology, while Socket.io is a library for WebSockets.
Socket.IO is way more than just a layer above WebSockets, it has different semantics (marks messages with name), and does failovers to different protocols, as well has heartbeating mechanism. More to that attaches ID's to clients on server side, and more. So it is not just a wrapper, it is full-featured library.
Here's a supplementary documentation for reference :
socket.emit('message', "this is a test"); //sending to sender-client only socket.broadcast.emit('message', "this is a test"); //sending to all clients except sender socket.broadcast.to('game').emit('message', 'nice game'); //sending to all clients in 'game' room(channel) except sender socket.to('game').emit('message', 'enjoy the game'); //sending to sender client, only if they are in 'game' room(channel) socket.broadcast.to(socketid).emit('message', 'for your eyes only'); //sending to individual socketid io.emit('message', "this is a test"); //sending to all clients, include sender io.in('game').emit('message', 'cool game'); //sending to all clients in 'game' room(channel), include sender io.of('myNamespace').emit('message', 'gg'); //sending to all clients in namespace 'myNamespace', include sender socket.emit(); //send to all connected clients socket.broadcast.emit(); //send to all connected clients except the one that sent the message socket.on(); //event listener, can be called on client to execute on server io.sockets.socket(); //for emiting to specific clients io.sockets.emit(); //send to all connected clients (same as socket.emit) io.sockets.on() ; //initial connection from a client.
Hope this helps!.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With