socket.io send packet to sender only

When your server listens, you usually get a socket at the “connection” event :

require('socket.io').on('connect', function(socket){

A socket connects 2 points : the client and the server. When you emit on this socket, you emit to this specific client.

Example :

var io = require('socket.io');
io.on('connect', function(socket){
    socket.on('A', function(something){
        // we just received a message
        // let's respond to *that* client :
        socket.emit('B', somethingElse);
    });
});

Be careful that those are two different calls :

  • socket.emit : emit to just one socket
  • io.sockets.emit : emit to all sockets

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)