83

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");
  });
});
Jack
  • 15,614
  • 19
  • 67
  • 92

6 Answers6

203

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);
madth3
  • 7,275
  • 12
  • 50
  • 74
Matt
  • 41,216
  • 30
  • 109
  • 147
  • 1
    thanks, is there any different between using broadcast, and using io.sockets.emit? – Jack Sep 08 '11 at 18:28
  • 4
    @Jack According to the documentation, "Broadcasting means sending a message to everyone else except for the socket that starts it.", where-as I would expect the other to send it to everyone. – Matt Sep 08 '11 at 18:52
  • 1
    It seems I made the mistake of providing a correction to this answer as an edit. Since the edit was rejected, here goes again: socket.broadcast.emit and io.sockets.emit are not alternatives to each other in the sense that they have a major difference. The broadcast variant emits to all sockets **except** to the trigger socket, whereas the io.sockets method truly emits to all sockets. – Tiago Espinha Jan 21 '13 at 14:35
  • Can the `io.sockets.emit` be used outside the scope of `io.sockets.on('connection', function (s){...` ? – vsync May 08 '13 at 15:00
  • @vsync Yes, so long as you have scope on the `io` object. – Matt May 08 '13 at 15:32
  • @Matt , what is clients in above lines. – Vinita Pawar Nov 16 '15 at 07:29
  • 2
    @Matt with latest Socket.IO API, io.sockets.emit does not work, use io.emit instead. – Gianluca Casati Jan 26 '18 at 18:59
14

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.

houkanshan
  • 140
  • 1
  • 6
3

As of socket.io version 0.9, "emit" no longer worked for me, and I've been using "send"

Here's what I'm doing:

Server Side:

var num_of_clients = io.sockets.clients().length;
io.sockets.send(num_of_clients);

Client Side:

ws = io.connect...
ws.on('message', function(data)
{
var sampleAttributes = fullData.split(',');
if (sampleAttributes[0]=="NumberOfClients")
        {
            console.log("number of connected clients = "+sampleAttributes[1]);
        }
});
Katie
  • 45,622
  • 19
  • 93
  • 125
  • 1
    You must be doing something wrong then... The current docs state "emit" http://socket.io/docs/ – Adam Aug 23 '15 at 21:30
1

According to this Broadcasting. With nodejs server, you can use this:

io.emit('event_id', {your_property: 'your_property_field'});

Be sure to initialise websocket, for example:

var express = require('express');
var http = require('http');
var app = express();
var server = http.Server(app);
var io = require('socket.io')(server);

app.get('/', function (req, res) {
    res.send('Hello World!');
    io.emit('event_hello', {message: 'Hello Socket'});
});

server.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

In this case, when user reach your server, there will be "event_hello" broadcasted to all web-socket clients with a json object {message: 'Hello Socket'}.

Tô Minh Tiến
  • 1,131
  • 11
  • 6
0

You can follow this example for implementing your scenario.

You can let all of clients to join a common room for sending some updates. Every socket can join room like this:

currentSocket.join("client-presence") //can be any name for room

Then you can have clients key in you sockets which contains multiple client's data(id and status) and if one client's status changes you can receive change event on socket like this:

socket.on('STATUS_CHANGE',emitClientsPresence(io,namespace,currentSocket); //event name should be same on client & server side for catching and emiting

and now you want all other clients to get updated, so you can do something like this:

emitClientsPresence => (io,namespace,currentSocket) {
  io.of(namespace)
        .to(client-presence)
        .emit('STATUS_CHANGE', { id: "client 1", status: "changed status" });
}

This will emit STATUS_CHANGE event to all sockets that have joined "client-presence" room and then you can catch same event on client side and update other client's status.

EBurkinshaw
  • 65
  • 1
  • 8
Rdavial
  • 29
  • 4
0

This worked for me:

io.in("room1").emit(/* ... */)

As explained in the socket.io emit cheatsheet: https://socket.io/docs/v3/emit-cheatsheet/

Philip Sopher
  • 627
  • 2
  • 8
  • 19