I am creating a chat application in Socket.io and node I am new in this I have a some query regarding chat app how can we maintain user's list and how can we send a message to specific person from user list?
Asked
Active
Viewed 2,158 times
0
-
Possible duplicate of [Send message to specific client with socket.io and node.js](https://stackoverflow.com/questions/4647348/send-message-to-specific-client-with-socket-io-and-node-js) – Nico Van Belle Sep 27 '17 at 13:37
-
i didn't find any appropriate answer that's why i am asking you can help me ? – aditi Sep 27 '17 at 13:43
-
[Then check the cheat sheet.](https://socket.io/docs/emit-cheatsheet) Not sure what you consider an *appropriate answer*. – Nico Van Belle Sep 27 '17 at 13:52
-
i checked but how can i get user socket.id ? – aditi Sep 27 '17 at 13:55
-
You really should read documentation before jumping to development. – Nico Van Belle Sep 27 '17 at 13:59
-
i read but didn't find appropriate solution so if you have any fast solution then please tell it's urgent – aditi Sep 27 '17 at 14:00
1 Answers
1
Like this (On the server-side) Cheatsheet:
// sending to individual socketid (private message)
socket.to(<socketid>).emit('hey', 'I just met you');
The <>
chatacters can be omitted, just make sure you replace socketid
with whatever the socket ID truly is. This will sent a message to ONLY the socket id specified. I know this works because I use it.

Retro Gamer
- 1,096
- 1
- 10
- 24
-
Thanks for help but how can i get socket id because when user refresh page then socket id will be change – aditi Sep 28 '17 at 05:18
-
If the user refreshes the page then the socket connection disconnects, and when they connect again they get a NEW socket id. You either need to prevent page refreshing if you are dealing with a form, or use some sort of unique identifier on the back-end that stores a user's info for when they reconnect again. If this is a chat app, I recommend AJAX. – Retro Gamer Sep 28 '17 at 15:24