Difference in Socket.io declaration

Asked

Viewed 64 times

3

I’m starting to work with Socket.io on Node.js, the doubt for now follows simple. Using my example below. What’s the difference in using socket.Emit() and chat.Emit()? PS:*I hope you don’t have a kkk syntax error.

var chat = io.of('/chat')
             .on('connection', function (socket) {
                 socket.emit('testOne', {
                    data: 'data'
                 });
                 chat.emit('testTwo', {
                    data2: 'data2'
                 });
             });

1 answer

2


socket.emit will issue the event to this specific socket, in case any client is connected and represents the socket.

In chat.emit the event is issued to all clients that are connected in the namespace /chat (which is created in io.of('/chat')).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.