-1

I am trying to understand the contexts of each of the following:

  • io.emit
  • io.socket.emit
  • io.sockets.emit
  • socket.emit
  • sockets.emit
  • socket.broadcast.emit

From what I understand:

The socket variable represents a single connection currently being communicated with.

The io variable represents the collection of socket variables

So far I've come up with the following. I have not figured out what the 2nd and 5th option are for. So maybe they do not exist. However, I am not sure what the difference is between the 1st and 3rd option:

  • io.emit - sends a message to all clients
  • io.socket.emit
  • io.sockets.emit - sends a message to all clients
  • socket.emit - sends a message to a single client
  • sockets.emit
  • socket.broadcast.emit - sends to all clients except sender

Is the 2nd and 5th option ever used? What is the difference between the 1st and the 3rd option?

kojow7
  • 10,308
  • 17
  • 80
  • 135
  • Possible duplicate of [Send response to all clients except sender](https://stackoverflow.com/questions/10058226/send-response-to-all-clients-except-sender) – Retro Gamer Oct 10 '17 at 20:32
  • This is not a duplicate of Retro Gamer's claimed duplicate. I had already mentioned to Retro Gamer that my question was based in part off of this page. He seems to have gotten offended by me indicating that his answer was not a good answer and then went ahead and posted his link as a duplicate to my question which it clearly is not. – kojow7 Oct 11 '17 at 02:51
  • Please look at the answers (1st & 2nd) to [this question](https://stackoverflow.com/questions/10058226/send-response-to-all-clients-except-sender). If you do a simple Ctrl-find, you will find answers to `io.emit`, `io.sockets.emit`, `socket.emit`, `sockets.emit`, `socket.broadcast.emit`. The only one that doesn't appear to have a description would be `io.socket.emit`. So obviously, your answer (most of it) is in the link stated above/below. Please understand that I am not offended, but that I am trying to prevent deplicate questions, as there are by far too many already in Stack Overflow. – Retro Gamer Oct 11 '17 at 15:56

2 Answers2

2

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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thank you. Also, my first question should have said "2nd and 5th" option, not "2nd and 3rd option". – kojow7 Oct 10 '17 at 15:03
  • @kojow7 - Well, I'm not aware of any variable named `sockets` (in your 5th option) so I'm not thinking that `sockets.emit()` works. Where did you get that notion from? – jfriend00 Oct 10 '17 at 23:53
  • In my search for various forms of emit I came across different references to 'sockets.emit()'. However, it seems that the people posting them may have made a mistake. An example is here: https://groups.google.com/forum/#!topic/socket_io/bjckmgTOQeE – kojow7 Oct 11 '17 at 02:48
  • 1
    @kojow7 - I think they just mean `io.sockets.emit()`, not `sockets.emit()`. There is no global variable named `sockets`. Someone was just referring to `sockets` as a shortcut for `io.sockets`. – jfriend00 Oct 11 '17 at 03:04
  • Thank you. And yes, you could be right. I had originally compiled a list of the different instances I had found. There are also places that talk about io.socket.emit such as https://medium.com/@abc810221/make-a-chat-room-with-socket-io-in-15-minutes-4986947904e5 - but perhaps that is just incorrect code. – kojow7 Oct 11 '17 at 04:18
  • @kojow7 - There's really no reason to go looking for an explanation for every possible line of code related to socket.io that you saw anywhere on the internet. There's lots and lots of junk on the internet. Stick to what is documented [here](https://github.com/socketio/socket.io/blob/master/docs/API.md) or what you see in well voted answers here or answers by high rep people and stick to solving an actual problem you have. You're making this a bit more difficult than it need be - looking for explanations for junk code. – jfriend00 Oct 11 '17 at 04:35
  • Fair enough. I started this trying to understand the idea of the emit method. So far I haven't found a guide that has them all in one place, but perhaps I was starting from the wrong perspective. – kojow7 Oct 11 '17 at 04:59
-2

Read the Documentation as well as the answers to this question. It has everything you need.

Retro Gamer
  • 1,096
  • 1
  • 10
  • 24
  • Your answer is not helpful. In fact, this is where I got some of my information from. Also, not everything I asked about is listed there. For example: `io.sockets.emit` is not listed there. – kojow7 Oct 10 '17 at 19:35
  • @kojow7 Well then take a look at this question: https://stackoverflow.com/questions/10342681/whats-the-difference-between-io-sockets-emit-and-broadcast a found that with a simple Google search. Why don't you just Google each of the emits you have a question on? – Retro Gamer Oct 10 '17 at 20:28
  • Or this one: https://stackoverflow.com/questions/10058226/send-response-to-all-clients-except-sender – Retro Gamer Oct 10 '17 at 20:31