1

I want to emit a action to a specific client using socket.io 1.0.

After reading Sending message to a specific ID in Socket.IO 1.0 , I know I can use:

io.sockets.connected[socketid].emit()

to emit a action to specific person.

But how can I get the socketid?

I used to write like this : socket.id=nickName, but it's wont work.

Community
  • 1
  • 1
Tony Chen
  • 501
  • 4
  • 16

1 Answers1

1

Simply access id property of socket object:

io.on('connection', function(socket) {
    console.log(socket.id);
});
Oleg
  • 22,300
  • 9
  • 68
  • 84
  • Ok, but why after i use 'socket.id = nickName', when i tried to emit to the new id i failed? is this socketid cannot to change? – Tony Chen Jun 12 '14 at 02:01
  • Yes `socket.id` should not be changed, but if you want you can write `socket.nickName = nickName` – Oleg Jun 12 '14 at 05:26