34

When I am using native websocket API I can see just a payload in my chrome console for sockts: enter image description here

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?

enter image description here

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', {...});

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753

2 Answers2

49

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

Mr. Tygrus
  • 499
  • 1
  • 4
  • 4
  • 1
    This was super useful... Just one question: any idea how we can send for example just a Pong response from client side to Socket.IO server's Ping? I've tried just sending "3" with no additional data but unfortunately that doesn't seem to work – orgg Jan 31 '21 at 12:10
15

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.

Community
  • 1
  • 1
Bogdan
  • 23,890
  • 3
  • 69
  • 61
  • How could you produce payloaded message with emit? Just tried to reproduce, but all message I gave were just a simple message, not payloaded. – mudlee Nov 21 '14 at 14:48