Broadcast message to all clients

I've tried broadcasting a message to all clients with no success.

The marked line WORKS but only to one client.

I've already tried these options: socket.emit socket.broadcast.emit

enter image description here

Thanks

You are calling the on function on the wrong object. You should rename the socket variable to something else. Most commonly, people use io.

io = require('socket.io').listen(server);

Then, you will need to call emit on sockets to send to all users:

io.sockets.on('connection', function(socket) {
    io.sockets.emit('message', data);
});

Of course, send is an emit that uses message as the event. So you can use it instead inside your callback:

io.sockets.send(data);

In either case, you then have to do the following in your client to react to this event:

socket.on('message', function (data) {
  // Do things with data
});

Note: socket.broadcast.emit can be used to send an event to all users connected other than the user that just connected as the socket.