TRomesh TRomesh - 1 month ago 15
Node.js Question

How to send personal messages using socket io and node express

im implementing a chat system where you can send personal messages to a particular person connected to a shocket.
first im putting the username as a key along with the shocket.id to a jason called "connectedUser".

socket.username=loggeduser;
connectedUser[socket.username]=socket.id;


Then im using this code send a personal message

socket.broadcast.to(connectedUser[usersnametobesent]).emit('chat', { message:result});


this works perfectly but whenever i use this it only shows the message on receiver's side but not the clients. i need it to be shown in both sender and receiver's side.

Answer

You will need to emit the same thing to the sender.

socket.emit('chat', {});

That function will only send to just that socket.

Why not just use some DOM manipulation to add the chat message to the clients side. This will also add a better user experience for if say there was bad lag for whatever reason, your sender would see the message he sent instantly and not rely on waiting to see his message return from the socket?

Here is a nice little cheat sheet for sockets:

 // sending to sender-client only
 socket.emit('message', "this is a test");

 // sending to all clients, include sender
 io.emit('message', "this is a test");

 // sending to all clients except sender
 socket.broadcast.emit('message', "this is a test");

 // sending to all clients in 'game' room(channel) except sender
 socket.broadcast.to('game').emit('message', 'nice game');

 // sending to all clients in 'game' room(channel), include sender
 io.in('game').emit('message', 'cool game');

 // sending to sender client, only if they are in 'game' room(channel)
 socket.to('game').emit('message', 'enjoy the game');

 // sending to all clients in namespace 'myNamespace', include sender
 io.of('myNamespace').emit('message', 'gg');

 // sending to individual socketid
 socket.broadcast.to(socketid).emit('message', 'for your eyes only');

Credit to http://stackoverflow.com/a/10099325


To answer your comment:

In the case of sockets, if you're sending a chat message say from a client so we got client1 and client2

Assuming you have a chat window you probably have some divs and maybe some lists like ul

Client1 sends to Client2 ---> "Hello"

Client1 should immediately see the message "Hello" by using simple Dom stuff like creating a new div / li and appending it to the chat window maybe with some sort of ID to keep track of messages.

Then if the message fails to send you can find that message by id that failed, remove it and maybe append an error message instead that says whatever, message failed to send.

Meanwhile client2 is none the wiser a message had ever been sent

If you use sockets to populate messages for both users then you might run into a case where

Client1 sends to Client2 ----> "Hello"

Now maybe your server has a hiccup or the client lost connection for a second for whatever reason, he doesn't see his message yet and goes oh maybe it didn't send so he goes

Client1 sends to Client2 ----> "Hello"

Client1 sends to Client2 ----> "Hello"

Client1 sends to Client2 ----> "Hello"

Still nothing, he does it 20 more times.

Suddenly the server or whatever unlocks and sends 300 hello messages both clients are spammed.

Sorry for formatting I'm on mobile.

Comments