socketio / socket.io-protocol

Socket.IO Protocol specification
https://socket.io
507 stars 62 forks source link

What do 420, 421, 422, 423, 424, 425 mean? And what protocol version? #27

Closed coolaj86 closed 1 year ago

coolaj86 commented 2 years ago

I'm trying to communicate with an EIO=3 socket.io server and I'm noticing that there's this 42x number showing up in front of the subscriptions after the length:

POST https://insight.dash.org/socket.io/?EIO=3&transport=polling&t=O5WgT6e&sid=vsxjXIoH1XiRiBLwBdSW

22:423["subscribe","inv"]23:424["subscribe","sync"]75:420["subscribe","dashd/addresstxid",["XoQVXRCQ6rR9s2EKh5GDMXR3JRtKCzt9pp"]]23:421["subscribe","sync"]23:425["subscribe","sync"]

For the life of me, I cannot figure out what this is supposed to mean, and I can't seem to find any documentation referencing this protocol version, nor can I find this pattern in the current versions of reference implementations that I've looked at.

It seems like the 42x number rotates, but not in any order I can understand.

I'd love some help deciphering what's going on here.

darrachequesne commented 2 years ago

Hi!

EIO=3 means:

The payload is encoded twice, once at the Socket.IO level, and then at the Engine.IO level. So, in your example:

22:423["subscribe","inv"]23:424["subscribe","sync"]23:424["subscribe","sync"] [...]

At the Engine.IO level:

part explanation
22 the length of the 423["subscribe","inv"] string (to split the payload into distinct packets)
: Engine.IO separator
4 Engine.IO "message" packet type
23["subscribe","inv"] the data in the Engine.IO packet

Then at the Socket.IO level:

part explanation
2 Socket.IO "event" packet type
3 the acknowledgement ID
["subscribe","inv"] the JSON.stringified version of the arguments, in that case socket.emit("subscribe", "inv")

The acknowledgement ID is a number that is incremented for each packet needing an acknowledgement:

socket.emit("subscribe", "inv", (response) => {
  // ...
});

Though the fact that they are out of order in your example is a bit surprising.

darrachequesne commented 1 year ago

I think this can be closed now, please reopen if needed.

coolaj86 commented 1 year ago

Yes.

I didn't see the notification about the answer at the time, but thank you so much @darrachequesne for such a great explanation.

I'm pretty sure I'll need to reference this again in the future, so I'm glad to have it.