0

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?

Aditi Parikh
  • 1,528
  • 3
  • 12
  • 33
aditi
  • 19
  • 7

1 Answers1

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
  • 928
  • 7
  • 21
  • 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