Skip to content

Commit 660263f

Browse files
docs: restore the upgrade mechanism in v4
After re-reading the goals of Engine.IO [1], it seems the upgrade mechanism is better for the user experience, so we'll keep that way. [1] https://github.com/socketio/engine.io#goals
1 parent cf3a759 commit 660263f

File tree

1 file changed

+52
-11
lines changed

1 file changed

+52
-11
lines changed

README.md

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ Table of Contents:
1818
- [2 ping](#2-ping)
1919
- [3 pong](#3-pong)
2020
- [4 message](#4-message)
21+
- [5 upgrade](#5-upgrade)
22+
- [6 noop](#6-noop)
2123
- [Payload](#payload)
2224
- [Transports](#transports)
2325
- [Polling](#polling)
2426
- [XHR](#xhr)
2527
- [JSONP](#jsonp)
2628
- [Server-sent events](#server-sent-events)
2729
- [WebSocket](#websocket)
28-
- [Transport fallback](#transport-fallback)
30+
- [Transport upgrading](#transport-upgrading)
2931
- [Timeouts](#timeouts)
3032
- [Difference between v3 and v4](#difference-between-v3-and-v4)
3133
- [Difference between v2 and v3](#difference-between-v2-and-v3)
@@ -126,7 +128,25 @@ hello => the 1st message
126128
world => the 2nd message
127129
```
128130

131+
- Request n°4 (WebSocket upgrade)
129132

133+
```
134+
GET /engine.io/?EIO=3&transport=websocket&sid=lv_VI97HAXpY6yYWAAAC
135+
< HTTP/1.1 101 Switching Protocols
136+
```
137+
138+
WebSocket frames:
139+
140+
```
141+
< 2probe => probe request
142+
> 3probe => probe response
143+
> 5 => "upgrade" packet type
144+
> 4hello => message (not concatenated)
145+
> 4world
146+
> 2 => "ping" packet type
147+
< 3 => "pong" packet type
148+
> 1 => "close" packet type
149+
```
130150

131151
## URLs
132152

@@ -219,6 +239,23 @@ actual message, client and server should call their callbacks with the data.
219239
1. client sends: ```4HelloWorld```
220240
2. server receives and calls callback ```socket.on('message', function (data) { console.log(data); });```
221241

242+
#### 5 upgrade
243+
244+
Before engine.io switches a transport, it tests, if server and client can communicate over this transport.
245+
If this test succeed, the client sends an upgrade packets which requests the server to flush its cache on
246+
the old transport and switch to the new transport.
247+
248+
#### 6 noop
249+
250+
A noop packet. Used primarily to force a poll cycle when an incoming websocket connection is received.
251+
252+
##### example
253+
1. client connects through new transport
254+
2. client sends ```2probe```
255+
3. server receives and sends ```3probe```
256+
4. client receives and sends ```5```
257+
5. server flushes and closes old transport and switches to new.
258+
222259
### Payload
223260

224261
A payload is a series of encoded packets tied together. The payload encoding format is as follows when only strings are sent and XHR2 is not supported:
@@ -356,13 +393,23 @@ already has a lightweight framing mechanism.
356393
In order to send a payload of messages, encode packets individually
357394
and `send()` them in succession.
358395

359-
## Transport fallback
396+
## Transport upgrading
360397

361-
A connection always starts with WebSocket (if supported by the client).
398+
A connection always starts with polling (either XHR or JSONP). WebSocket
399+
gets tested on the side by sending a probe. If the probe is responded
400+
from the server, an upgrade packet is sent.
362401

363-
If the connection cannot be established, the client will try to establish a SSE stream.
402+
To ensure no messages are lost, the upgrade packet will only be sent
403+
once all the buffers of the existing transport are flushed and the
404+
transport is considered _paused_.
364405

365-
If the connection still fails, the client will use polling as a fallback (either XHR or JSONP).
406+
When the server receives the upgrade packet, it must assume this is the
407+
new transport channel and send all existing buffers (if any) to it.
408+
409+
The probe sent by the client is a `ping` packet with `probe` sent as data.
410+
The probe sent by the server is a `pong` packet with `probe` sent as data.
411+
412+
Moving forward, upgrades other than just `polling -> x` are being considered.
366413

367414
## Timeouts
368415

@@ -386,12 +433,6 @@ does not receive any data within `pingTimeout + pingInterval`.
386433
The ping packets will now be sent by the server, because the timers set in the browsers are not reliable enough. We
387434
suspect that a lot of timeout problems came from timers being delayed on the client-side.
388435

389-
- fallback to polling instead of upgrading to WebSocket
390-
391-
As of today, most browsers now support WebSocket: https://caniuse.com/#search=websocket
392-
393-
Please note that this does not remove the need to use sticky-session for the polling transport.
394-
395436
- always use base64 when encoding a payload with binary data
396437

397438
This change allows to treat all payloads (with or without binary) the same way, without having to take in account

0 commit comments

Comments
 (0)