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.

Kerem Çakır
  • 401
  • 2
  • 7
  • 19

1 Answers1

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)
David
  • 1,829
  • 20
  • 31