emit to someone

196 views
Skip to first unread message

于建

unread,
Dec 26, 2012, 1:20:15 AM12/26/12
to sock...@googlegroups.com
groups,

I can use socket.broadcast.emit() to anyone,but how can i emit to someone?
please help me.


Sergio Canales

unread,
Dec 26, 2012, 5:58:21 PM12/26/12
to sock...@googlegroups.com
Just do socket.emit()

Josh Kamau

unread,
Dec 26, 2012, 5:59:55 PM12/26/12
to sock...@googlegroups.com
"Just do socket.emit() "  where "socket" is someones socket

于建

unread,
Dec 26, 2012, 8:27:29 PM12/26/12
to sock...@googlegroups.com
"Just do socket.emit() " how can i get the someones socket?
I try to use "io.sockets[someones socket's id].emit", but can not work...
* the socket's id save to a map at server.

于建

unread,
Dec 26, 2012, 10:38:17 PM12/26/12
to sock...@googlegroups.com
I can do it.

io.sockets.socket(someone socket's id).emit("event name", msg);

test OK!

Sergio Canales

unread,
Dec 26, 2012, 10:42:12 PM12/26/12
to sock...@googlegroups.com
If you do socket.emit() you'll have the same as io.socket.sockets(socket.id).emit(), just get the socket object around on the 

.on('connection', function(socket){

});

 or 

.on('message', function(socket, data){

});

S.

Sent from my iPhone


Nicolas Chambrier

unread,
Dec 29, 2012, 6:10:10 AM12/29/12
to socket_io
The other way would be to use rooms for that
--
Nicolas Chambrier, aka naholyr

Blog : http://naholyr.fr
Formateur Clever Institut : http://clever-institut.com/formateur/nicolas-chambrier

Nicolas Chambrier

unread,
Dec 29, 2012, 6:11:06 AM12/29/12
to socket_io
Sorry for double post but I was not sure: by the way, is "io.sockets.socket(id)" cluster-proof?

Kamil Krzyszczuk

unread,
Dec 29, 2012, 7:54:18 AM12/29/12
to sock...@googlegroups.com
only if u sync by for example Redis, or socket.io-clusterhub, I recommend you first solution, second one break when there is reconnection, don't know why.


2012/12/29 Nicolas Chambrier <nah...@gmail.com>

Nicolas Chambrier

unread,
Dec 29, 2012, 4:25:05 PM12/29/12
to socket_io
K thanks, good to know cause I noticed "io.sockets[id]" was not reliable when using cluster & redis (maybe that changed)

于建

unread,
Dec 30, 2012, 9:19:40 AM12/30/12
to sock...@googlegroups.com
I am so sorry ,i didn't know what is "cluster-proof".
But i know "io.socket.sockets(socket.id).emit()" can do it.
A only send msg to B.

于建

unread,
Dec 30, 2012, 9:25:43 AM12/30/12
to sock...@googlegroups.com
Hi N,
How can i do it by "rooms "?

Phoungeun Vantho

unread,
Dec 26, 2012, 8:43:48 PM12/26/12
to sock...@googlegroups.com
have a look at that url below that shows you how to broadcast or target a specific socket.

Felix Genicio Calvo

unread,
Dec 26, 2012, 2:23:57 AM12/26/12
to sock...@googlegroups.com
You can store some kind of nick in an array, associated to the connection id. Later you can send messages to that connection using the nick. 

socket.on('connection', function (nick) {
   nicknames[nick] = socket.id
}

socket.on('message', function (text,nick) {                                                                                         
    io.sockets.socket(nicknames[nick]).emit('message',text);                                                                        
}); 


2012/12/26 于建 <ic...@126.com>

Farez Ramilo

unread,
Dec 26, 2012, 6:20:45 PM12/26/12
to sock...@googlegroups.com
for sending to a specific socket use io.sockets.socket(socket.id)
where the 'socket' obj can be obtained from your client's connection event
io.sockets.on('connection', function (socket) {
...
});

be careful not to confuse between 'sockets' and 'socket'

Amit Kumar Maurya

unread,
Dec 26, 2012, 1:24:43 AM12/26/12
to sock...@googlegroups.com

于建

unread,
Jan 2, 2013, 8:13:16 PM1/2/13
to sock...@googlegroups.com

P,
Happy New Year!
Thank you for your help.

于建

unread,
Jan 2, 2013, 8:19:26 PM1/2/13
to sock...@googlegroups.com
Hi,F
Happy New Year!
I can get socket when every "connection".and I save every socket it to  array.
when i want to use A sent to B,Can do it by " io.sockets.socket(socket.id)".

于建

unread,
Jan 2, 2013, 8:28:30 PM1/2/13
to sock...@googlegroups.com
Hi,M
Happy New Year!
Thank you for your help.

socket.join(session.username);
io.sockets.in("aFancyUsername").emit('yourEventName',{foo : "bar"});

I will try it.


At 2012-12-31 23:14:06,"Mattia Alfieri" <cikk...@gmail.com> wrote:

Using expressjs and assuming that your application uses sessions and assigns usernames,you can make the user's socket join a particular room named as the user.
I am using a npm module named session.socket.io which makes the session object available inside socket.io callbacks.

You can find the example code here : https://gist.github.com/4378940


Hope this helps you.
Reply all
Reply to author
Forward
0 new messages