Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update all clients using Socket.io?

Is it possible to force all clients to update using socket.io? I've tried the following, but it doesn't seem to update other clients when a new client connects:

Serverside JavaScript:

I'm attempting to send a message to all clients, which contains the current number of connected users, it correctly sends the amount of users.... however the client itself doesn't seem to update until the page has been refreshed. I want this to happen is realtime.

var clients = 0; io.sockets.on('connection', function (socket) {   ++clients;   socket.emit('users_count', clients);       socket.on('disconnect', function () {     --clients;   }); }); 

Clientside JavaScript:

var socket = io.connect('http://localhost');  socket.on('connect', function(){   socket.on('users_count', function(data){     $('#client_count').text(data);     console.log("Connection");   }); }); 
like image 555
Jack Avatar asked Sep 08 '11 17:09

Jack


People also ask

Is Socket.IO better than WebSocket?

As said before, Socket.IO can fall back to technologies other than WebSockets when the client doesn't support it. If (for some reason) a WebSocket connection drops, it will not automatically reconnect… but guess what? Socket.IO handles that for you! Socket.IO APIs are built to be easier to work with.

Is Socket.IO difficult?

All in all, the Socket.io code was incredibly simple. My app. js for my Express app is below — it simply listens for connections and pixels changing colors.

How do I use Socket.IO in client side?

var socket = require('socket. io-client')('ws://ws.website.com/socket.io/?EIO=3&transport=websocket'); socket. on('connect', function() { console. log("Successfully connected!"); });

How many users Socket.IO can handle?

Once you reboot your machine, you will now be able to happily go to 55k concurrent connections (per incoming IP).


2 Answers

It's not actually sending an update to the other clients at all, instead it's just emitting to the client that just connected (which is why you see the update when you first load)

// socket is the *current* socket of the client that just connected socket.emit('users_count', clients);  

Instead, you want to emit to all sockets

io.sockets.emit('users_count', clients); 

Alternatively, you can use the broadcast function, which sends a message to everyone except the socket that starts it:

socket.broadcast.emit('users_count', clients); 
like image 122
Matt Avatar answered Oct 20 '22 11:10

Matt


I found that using socket.broadcast.emit() will only broadcast to the current "connection", but io.sockets.emit will broadcast to all the clients. here the server is listening to "two connections", which are exactlly 2 socket namespaces

io.of('/namespace').on('connection', function(){     socket.broadcast.emit("hello"); }); io.of('/other namespace').on('connection',function(){/*...*/}); 

i have try to use io.sockets.emit() in one namespace but it was received by the client in the other namespace. however socket.broadcast.emit() will just broadcast the current socket namespace.

like image 20
houkanshan Avatar answered Oct 20 '22 12:10

houkanshan