When I am using native websocket API I can see just a payload in my chrome console for sockts:
But when I use socket.io with their emit event, I can see some strange numbers before my actual payload. I do understand that colors mean that you either send or received the data, but what does the numbers like 42, 3, 2, 430, 420, 5
mean.
Is there a place I can get a full list of these numbers with descriptions?
The code which generates it is kind of big, so I just post small snippets.
Client side always look like this:
socket.emit('joinC', room, function(color){ ... });
Server side looks like this:
io.sockets.in(room).emit('moveS', {...});
As of v3, socket.io has a default message limit of 1 MB. If a message is larger than that, the connection will be killed.
Both server and client node processes use 95-100% of a CPU core each. So pure throughput looks ok. I can emit 100 messages per second to 100 local clients at 55% CPU usage on the server process.
socket.io rooms are a lightweight data structure. They are simply an array of connections that are associated with that room. You can have as many as you want (within normal memory usage limits). There is no heavyweight thing that makes a room expensive in terms of resources.
This is an old post, but it's worth noting that Socket.io over WebSockets actually does guarantee event ordering is maintained. This is because TCP itself, which is the underlying technology for WebSockets & HTTP guarantees packet ordering to be maintained.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With