0

I use express, socket.io.

I have two user types

  1. site visitors (Site users)

  2. admins (site admins)

When visitors visit the site, i add user data(browser, location, ip..etc..) to VisitorObject and show the VisitorObject to admins.

When admins want to send message to a visitor, i should emit function of only user which the admin wants to type.

I don't want to use

io.sockets.emit('newMessage', message); 

I want to use like this

io.sockets[id].emit('newMessage', message); 

EDIT---

i am connection node.js server with

 var socket = io.connect('http://127.0.0.1:3000', {query: 'id=' + id}); 

This id is not a socket id, it is the user id. I don't want to use socket.id on send message. I want to use user id. Because socket.id always change for the tab of browser.

2
  • do you want to send message to specific socket (by socket id ) ? Commented Feb 8, 2018 at 11:12
  • yes i want .... Commented Feb 8, 2018 at 11:16

1 Answer 1

2

try something like this

 // sending to individual socketid (private message) socket.to(<socketid>).emit('newMessage', message); 

or

io.to(<socketid>).emit('newMessage', message); 

https://socket.io/docs/emit-cheatsheet/

if you need to emit by user_id you can do this

socket.join(user_id); // when user connected 

and than

io.to(user_id).emit('newMessage', message) 
Sign up to request clarification or add additional context in comments.

1 Comment

i'm sorry, i don't want to use socket.id. I want to use user id.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.