Rajesh
Rajesh

Reputation: 123

How to send request to all connected clients in Websocket

I am quite new to Websocket Communication and Node.js. Can anyone help me how to send request to all clients connected through Websocket.

socket = io.connect('http://localhost:8000/');
socket.on('user_in', function (data){
        console.log(data);
        socket.send("Hi All");
  });

  socket.on('user_out', function (data){
        console.log(data);
        socket.send("Hi All");
  });

  socket.on('to_all', function (data){
        console.log(data);
        socket.send("Hi All");
  });

Upvotes: 0

Views: 707

Answers (1)

ldmat
ldmat

Reputation: 1041

It looks like you're trying to broadcast messages from a Socket.IO client to all other clients. As far as I know that can't be done, but you can do it from the server:

io.on('connection', function (socket) {

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

});

There's a good example here.

Upvotes: 1

Related Questions