How to send a message to a particular client with socket.io

前端 未结 6 1978
無奈伤痛
無奈伤痛 2020-11-28 00:02

I\'m starting with socket.io + node.js, I know how to send a message locally and to broadcast socket.broadcast.emit() function:- all the connected clients recei

相关标签:
6条回答
  • 2020-11-28 00:48

    You can use socket.io rooms. From the client side emit an event ("join" in this case, can be anything) with any unique identifier (email, id).

    Client Side:

    var socket = io.connect('http://localhost');
    socket.emit('join', {email: user1@example.com});
    

    Now, from the server side use that information to create an unique room for that user

    Server Side:

    var io = require('socket.io').listen(80);
    
    io.sockets.on('connection', function (socket) {
      socket.on('join', function (data) {
        socket.join(data.email); // We are using room of socket io
      });
    });
    

    So, now every user has joined a room named after user's email. So if you want to send a specific user a message you just have to

    Server Side:

    io.sockets.in('user1@example.com').emit('new_msg', {msg: 'hello'});
    

    The last thing left to do on the client side is listen to the "new_msg" event.

    Client Side:

    socket.on("new_msg", function(data) {
        alert(data.msg);
    }
    

    I hope you get the idea.

    0 讨论(0)
  • 2020-11-28 00:52

    When a user connects, it should send a message to the server with a username which has to be unique, like an email.

    A pair of username and socket should be stored in an object like this:

    var users = {
        'userA@example.com': [socket object],
        'userB@example.com': [socket object],
        'userC@example.com': [socket object]
    }
    

    On the client, emit an object to the server with the following data:

    {
        to:[the other receiver's username as a string],
        from:[the person who sent the message as string],
        message:[the message to be sent as string]
    }
    

    On the server, listen for messages. When a message is received, emit the data to the receiver.

    users[data.to].emit('receivedMessage', data)
    

    On the client, listen for emits from the server called 'receivedMessage', and by reading the data you can handle who it came from and the message that was sent.

    0 讨论(0)
  • 2020-11-28 00:53

    In a project of our company we are using "rooms" approach and it's name is a combination of user ids of all users in a conversation as a unique identifier (our implementation is more like facebook messenger), example:

    |id | name |1 | Scott |2 | Susan

    "room" name will be "1-2" (ids are ordered Asc.) and on disconnect socket.io automatically cleans up the room

    this way you send messages just to that room and only to online (connected) users (less packages sent throughout the server).

    0 讨论(0)
  • 2020-11-28 00:56

    You can refer to socket.io rooms. When you handshaked socket - you can join him to named room, for instance "user.#{userid}".

    After that, you can send private message to any client by convenient name, for instance:

    io.sockets.in('user.125').emit('new_message', {text: "Hello world"})

    In operation above we send "new_message" to user "125".

    thanks.

    0 讨论(0)
  • 2020-11-28 01:00

    SURE: Simply,

    This is what you need :

    io.to(socket.id).emit("event", data);
    

    whenever a user joined to the server, socket details will be generated including ID. This is the ID really helps to send a message to particular people.

    first we need to store all the socket.ids in array,

    var people={};
    
    people[name] =  socket.id;
    

    here name is the receiver name. Example:

    people["ccccc"]=2387423cjhgfwerwer23;
    

    So, now we can get that socket.id with the receiver name whenever we are sending message:

    for this we need to know the receivername. You need to emit receiver name to the server.

    final thing is:

     socket.on('chat message', function(data){
    io.to(people[data.receiver]).emit('chat message', data.msg);
    });
    

    Hope this works well for you.

    Good Luck!!

    0 讨论(0)
  • 2020-11-28 01:09

    As the az7ar answer is beautifully said but Let me make it simpler with socket.io rooms. request a server with a unique identifier to join a server. here we are using an email as a unique identifier.

    Client Socket.io

    socket.on('connect', function () {
      socket.emit('join', {email: user@example.com});
    });
    

    When the user joined a server, create a room for that user

    Server Socket.io

    io.on('connection', function (socket) {
       socket.on('join', function (data) {    
        socket.join(data.email);
      });
    });
    

    Now we are all set with joining. let emit something to from the server to room, so that user can listen.

    Server Socket.io

    io.to('user@example.com').emit('message', {msg: 'hello world.'});
    

    Let finalize the topic with listening to message event to the client side

    socket.on("message", function(data) {
      alert(data.msg);
    });
    

    The reference from Socket.io rooms

    0 讨论(0)
提交回复
热议问题