What do these numbers mean in socket.io payload?
I know you asked a while ago, but the information remains for those who are researching.
I did an analysis with reverse engineering in version 2.3.0 (socket.io) and 3.4.2 (engine.io) and got the following:
The first number is the type of communication for engine.io, using the enumerator:
Key | Value |
---|---|
0 | "open" |
1 | "close" |
2 | "ping" |
3 | "pong" |
4 | "message" |
5 | "upgrade" |
6 | "noop" |
The second number is the type of action for socket.io, using the enumerator
Key | Value |
---|---|
0 | "CONNECT" |
1 | "DISCONNECT" |
2 | "EVENT" |
3 | "ACK" |
4 | "ERROR" |
5 | "BINARY_EVENT" |
6 | "BINARY_ACK" |
There are other optional information that can be passed on, such as namespace and ID, but I will not go into that part.
After these codes he expects a Json Array, where index 0 is the name of the event and index 1 is the argument.
So the instruction 42["moveS",{"from":"g1", "to", "f3"}]
is a message for engine.io (4), is an event for socket.io (2), which will emit the "moveS" action passing JSON {"from": "g1", "to", "f3"}
as a parameter(Actually JSON.Parse({"from": "g1", "to", "f3"})
).
Hope this helps. =D
Websockets allow you to send data back and forth over a full-duplex communication channel.
Socket.IO on the other hand is a realtime application framework that uses websockets as transport adding features like namespacing connections, rooms, fallback to other transports etc. To build all those features, the messages exchanged back and forward must cary some semantics so that Socket.IO knows what they mean (i.e. what kind of message is it, event, error etc) and what to do with them. For that it uses a protocol that frames the message with some codes that identify it's semantic. That's what you are seeing with those numbers.
Unfortunately the Socket.IO documentation is very terse and it's hard to understand exactly how those codes are combined and parsed. To get their exact meaning I think one needs to look at the Socket.IO source code.
EDIT from a socket.io Github issue:
This is handled in socket.io-parser and engine.io-parser, which are implementations of socket.io-protocol and engine.io-protocol respectively. You can find the protocol description for socket.io here and for engine.io here.
The encoding sections in these documents are of interest when looking at the actual data that is sent through the transports. The socket.io-protocol handles encoding of metadata, like namespaes to an engine.io-protocol handleable format.