0

I have tried this link: Socket.IO subscribe to multiple channels.

I have used method socket.on and io.emit for request and response respectively.

Currently I have facing issue that I got response to all users.
I want to just notify for particular user.

0

1 Answer 1

1

When a user connects, it should send a message to the server with a username which has to be unique.

A pair of username and socket should be stored in an object like this:

var users = {
   '[email protected]': [socket object], // USER SOCKET OBJECT
   '[email protected]': [socket object], // USER SOCKET OBJECT
   '[email protected]': [socket object]  // USER SOCKET OBJECT
}

On the client, emit an object to the server with the following data:

{
   to:[receiver's username],
   from:[the person who sent the message],
   message:[the message to be sent],
   //..other details..
}

On the server, listen for messages. When a message is received, emit the data to the receiver.

users[data.to].emit('new-message', data.message)

On the client, you will need to listen to new-message.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.