How can I send a message to a particular client with socket io

0 votes
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 receive the same message.

Now, I would like to know how to send a private message to a particular client, I mean one socket for a private chat between 2 person
Apr 27, 2020 in Java-Script by kartik
• 37,520 points
27,829 views

1 answer to this question.

0 votes

Hii,

You can try the code below:

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.

Hope this work!!

Thank You!!

answered Apr 27, 2020 by Niroj
• 82,840 points

Related Questions In Java-Script

0 votes
1 answer

How to send data in request body with a GET when using jQuery $.ajax()?

Hello @kartik, Sending the data in your scenario,I ...READ MORE

answered Jun 18, 2020 in Java-Script by Niroj
• 82,840 points
19,526 views
0 votes
1 answer

How can I check whether a radio button is selected with JavaScript?

Hello @kartik, You can use vanilla JavaScript way var radios ...READ MORE

answered Sep 3, 2020 in Java-Script by Niroj
• 82,840 points
2,181 views
0 votes
1 answer

How can I make a div stick to the top of the screen once it's been scrolled to?

Hello @kartik, Using javascript: var initTopPosition= $('#myElementToStick').offset().top; ...READ MORE

answered Sep 4, 2020 in Java-Script by Niroj
• 82,840 points
830 views
0 votes
1 answer

How do I turn a string to a json in Node.js?

Hello Kartik, Use the JSON function  JSON.parse(theString) ...READ MORE

answered Apr 24, 2020 in Java-Script by Niroj
• 82,840 points
975 views
+1 vote
1 answer

What is the relationship between angularjs Scope with controller/view?

Let us consider the below block: <div ng-controller="emp"> ...READ MORE

answered Jan 20, 2020 in Web Development by Niroj
• 82,840 points

edited Jan 21, 2020 by Niroj 1,144 views
0 votes
1 answer

What is data binding in AngularJS?

Data binding is synchronization of data between the ...READ MORE

answered Jan 23, 2020 in Web Development by Niroj
• 82,840 points
1,121 views
0 votes
1 answer

How can we avoid my php form from hacking?

Hii @kartik, If you want to know php ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,840 points
2,593 views
0 votes
1 answer

How can you display the error messages?

Hey, In the HTML form, we add ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,840 points
3,484 views
+1 vote
1 answer

How can we send message multiple time to a specific person or group in whatsapp using loop?

Hii @kartik,  This is simple task to send single ...READ MORE

answered Feb 28, 2020 in Java-Script by Niroj
• 82,840 points
18,627 views
0 votes
1 answer

How can I allow django admin to set a field to NULL?

Hello @kartik, Try to overwrite the save() method ...READ MORE

answered Jun 12, 2020 in Java-Script by Niroj
• 82,840 points
4,809 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP