Trojan
Trojan

Reputation: 1456

Send a message to specific people including sender using socket.io in nodejs

How to send a message to particular client including sender..

The following is the code i used to send a message to particular client.

socket.on('chat message', function(msg){
io.to(socket.id).emit('chat message', msg);
});

Ofcourse its working But it just sending to the client(reciever) only.But i want to send a message to particular client(reciever) including sender also.

Is anything need to add in that code.?? suggestions are welcome.!!

Thanks,

Upvotes: 0

Views: 1713

Answers (1)

Devang Tandel
Devang Tandel

Reputation: 3008

There are 2 ways to achieve same from server end

1) Emit event to both user separately 2) Broadcast event to array of user(In your case senderId, reciverId)

Here i am enclosing broadcast function considering you are already aware with emit.

client.broadcast.to(arrayOfIds).emit('eventName', {
                data: "somedata"
            });

Here, arrayOfIds : Array of Id of Recivers, i.e.(Id Of sender and Id Of receiver)

This function with send event to both user, including sender him self.

Upvotes: 1

Related Questions