简体   繁体   中英

Socket.io 1.x + Express (using http): How to send messages to specific client id?

I'm new to socket.io, and I'm doing a simple API with NodeJS (express 4). I'm developing an action that is similar to the old "poke" action at facebook. A user send a poke to other user, and this one gets a notification on real time (this is the reason why I am using socket.io).

This is the code:

app.js

var port   = 3000;
var app    = module.exports = express();
var server = require('http').Server(app);
...
server.listen(port);
require('./config/socket-io')(app, server, secret);

socket-io.js

module.exports = function(app, server, secret) {
    var clients = {};
    console.log("initiating sockets...");
    var sio = require('socket.io').listen(server, {'log level': 2});

    sio.on('connection', function (socket) {
        console.log("...new connection: "+socket.client.id);
        clients[socket.id] = socket;
        socket.emit('identification', { data : socket.client.id });

        socket.on('newShoutOut', function(data) {
            var receptor    = data.idTo;
            var emiter      = socket.client.id;
            console.log("...new shout out from " +emiter+ " to "+receptor);
            sio.sockets.sockets[receptor].emit({ data : data.data, from : emiter });
        });

        socket.on('disconnect', function() {
            console.log("..."+socket.client.id + " disconnected");
        });
    });
};

Here you can differentiate three states:

  • Connection: The server detects all the clients connection to the host:port. After that, the server sends to each client his ID. This works fine.

  • Send message: One client sends a notification to other client. For now, the server receives the notification from one client, but the "receiver" doesn't receive anything.

  • Disconnection: Doesn't matter in this case.

My question is, what is the way to send a message to a client directly knowing the ID? What I am doing wrong? I tried so many options to send a message directly to a specific client ID but didn't work...

EDIT

Frontend

var socket = io('http://localhost:3000');
var id = "";
socket.on('connection', function (data) {
    console.log("connected!");
    console.log(data);
});
socket.on('identification', function(data) {
    id = data.data;
    $("#socket_info h1").html("ID: "+id);
});
socket.on('newShoutOut', function(data) {
    console.log("newShoutOut received!");
});

Ok, so I assume the shoutout is coming from a user? You will need to create the event on the clientside, such as:

var button = $('#button');

button.on('click', function() {
    var msg = 'message',
        userID = '123'; //get the ID who they are messaging

    socket.emit('sendShoutOut', {msg: msg, id: userID});
});

Then you will need to receive that response on the server, and reply to the user in that function:

socket.on('sendShoutOut', function( data ) {

    socket.sockets.sockets[data.id].emit('sendPrivateMsg', { data : data.msg, from : emiter });
});

Lastly, the reciever must be notified, so you will need to handle the response on the client:

socket.on('sendPrivateMsg', function( data ) { 
    alert(data);    
});

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM