0

As far as I know, unless told otherwise, if the server sends a message, all clients should receive it. But in my case only one client is getting the messages.

client :

(function () {
  window.Network = {
    socket : null,  
    initialize : function(socketURL) {
      this.socket = io.connect(socketURL);
      this.socket.on('new move', this.add);     
    },  
   add : function(data) {   
      var msg = $('<div class="msg"></div>')       
      .append('<span class="text">' + data.x+'/'+data.y + '</span>');

      $('#messages')
        .append(msg)
        .animate({scrollTop: $('#messages').prop('scrollHeight')}, 0);
    },   
   send : function(data) {
      this.socket.emit("move",data);
      return false;
    }
  };
}());

server:

io.sockets.on('connection', function (socket) {
    socket.on('move', function (data) {
    console.log(data);
    socket.emit("new move",data);
  }); 
});

If I open several clients and use "send" function, only the client that sent it receives the emit from the server. Any idea what I am doing wrong? Thanks

Kal
  • 323
  • 2
  • 7
  • 17

3 Answers3

3

To emit globally on the server side use this :

io.sockets.emit('new move', 'data1');

To emit to the current socket :

socket.emit('function', 'data1', 'data2');

To broadcast to everyone but the client :

socket.broadcast.emit('function', 'data1', 'data2');

Source

soyuka
  • 8,839
  • 3
  • 39
  • 54
  • I just read a post saying the same as you do http://stackoverflow.com/questions/9837998/socket-io-client-not-receiving-messages-from-server?rq=1 Tested it changing my code to io.sockets.emit("new user",data); But still, only the sender receives the emit from the server – Kal Feb 13 '13 at 08:39
2

When you are using socket, you are talking directly to the connected client.

You can use rooms however, un this snippet i add the socket to room1

// Add the player socket, to the room.
socket.join('room1');

Then you can emit to all client in a room by

io.sockets.in('room1').emit('startGame', true);
MartinElvar
  • 5,695
  • 6
  • 39
  • 56
  • This wokred too. I will leave it simple for now, but may create rooms later. Thanks for the info. – Kal Feb 13 '13 at 08:49
  • 1
    +1000 I was looking for this for 3 days. Why isn't it clearly stated in the docs of btford/angular-socket-io or socket.io? – ajbraus Oct 24 '15 at 22:10
0

The code is working just as it is supposed to. You are doing socket.emit("new move",data);, which will emit a new move event back to that specific socket, which in your case is the same socket that connected.

If you also want it to be sent to other clients that are connected, then you need to explicitly emit the event to other clients.

socket.emit("new move",data);
socket.broadcast.emit("new move",data);
loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
  • But in this example http://tech.pro/tutorial/1097/simple-chat-nodejs-plus-websockets they don't use broadcast and all clients get the chat messages. I even got it working on local and the code looks the same to mine. – Kal Feb 13 '13 at 08:37