0

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.

TRomesh
  • 4,323
  • 8
  • 44
  • 74

2 Answers2

2

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 https://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.

Community
  • 1
  • 1
Datsik
  • 14,453
  • 14
  • 80
  • 121
  • 1
    im kinda new to this shocketio thing can you explain a bit more? and how to use DOM manipulation to add the chat message to the clients side? – TRomesh Feb 26 '16 at 16:58
  • 1
    thanks got it working :) and the cheat sheet was useful – TRomesh Mar 01 '16 at 16:13
1

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 reciever name. Example:

 people["ccccc"]=2387423cjhgfwerwer23;

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

for this we need to know the recievername.You need to emit reciever name to the server.

final thing is:

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

Hope this works well for you.!!Good Luck

Trojan
  • 1,396
  • 2
  • 16
  • 19
  • awesome i did some thing similar to this and it worked out pretty well. Anyway was expecting an answer much earlier :D – TRomesh Oct 22 '16 at 08:47