The ones that I see that are documented are:
io.emit() - send to all connected clients
io.sockets.emit() - send to all clients in the "/" namespace
io.to(roomName).emit() - send to all clients in a particular room
io.in(roomName).emit() - .in() is the same as .to()
io.of(nsp).emit() - send to all clients in a particular namespace
io.of(nsp).to(room).emit() - send to clients in a namespace that are in a room
namespace.emit() - send to all clients in a particular namespace
socket.emit() - send to single client
socket.broadcast.emit() - send to all connected clients except socket
io.sockets
is the default /
namespace so it's a special case of io.of('/')
. So, io.sockets.emit()
is just emitting to a namespace as in io.of('/').emit()
.
If your clients aren't connecting to any custom namespaces, then all your client connections will be in the /
namespace so io.emit()
and io.sockets.emit()
will end up doing the same thing. But, if you have any clients connecting to a custom namespace, then io.sockets.emit()
will only be sending to sockets in the /
namespace whereas io.emit()
will send to all connected clients regardless of namespace.
I'm not aware of a sockets
variable or a io.socket
property. If they actually exist, they do not appear to be documented and I would not recommend using them.
Is the 2nd and 3rd option ever used?
I'm not aware of support for io.socket.emit()
. io.sockets.emit()
broadcast to all clients connected to the default namespace which does have a use when there are other namespaces being used.
What is the difference between the 1st and the 3rd option?
As I explained above, there is a different between io.emit()
and io.sockets.emit()
when there are any clients connecting to a custom namespace.