Socket.IO v4 client reconnection timing inconsistency after WebSocket handshake failure
0 reputation · 03 Feb 2020, 15:53 UTC
0 reputation · 03 Feb 2020, 15:53 UTC
Goal: understand whether the client should automatically switch to long‑polling when a WebSocket handshake is blocked by a restrictive proxy, and how this decision influences the reconnection schedule.
Constraint: Socket.IO v4 delegates transport selection to Engine.IO, which does not expose an explicit fallback‑to‑long‑polling flag; consequently, reconnection attempts may vary between environments where proxies allow WebSocket and those that force long‑polling, leading to unpredictable latency.
Questions:
35275 reputation · 03 Feb 2020, 18:46 UTC
Socket.IO v4 does not expose a client‑side flag to force long‑polling only after a WebSocket handshake failure; the fallback is handled automatically by Engine.IO.
transports option (e.g., { transports: ['polling'] }).transports is omitted or includes both ['websocket','polling'], Engine.IO tries WebSocket first and falls back to polling only if the WebSocket upgrade fails.reconnectionAttempts, reconnectionDelay, reconnectionDelayMax, and randomizationFactor.If a corporate proxy blocks the Upgrade: websocket header, the initial WebSocket handshake fails, Engine.IO detects the error and switches to the next transport in the allowed list (usually polling). Because this switch happens per‑connection attempt, the observed reconnection interval can vary between environments where the proxy allows WebSocket (fast reconnect) and those that force polling (extra HTTP round‑trips and longer delay).
const socket = io(serverUrl, {
transports: ['polling'], // force polling only
reconnectionAttempts: 5,
reconnectionDelay: 1000,
reconnectionDelayMax: 5000,
randomizationFactor: 0.5
});
const socket = io(serverUrl, {
transports: ['websocket','polling'],
reconnectionAttempts: 10,
reconnectionDelay: 2000,
reconnectionDelayMax: 10000,
randomizationFactor: 0
});
socket.io.engine.on('upgrade', (transport) => {
console.log('Transport upgraded to', transport.name);
});
socket.io.engine.on('error', (err) => {
console.error('Transport error', err);
});
Can you confirm whether the proxy in your environment blocks the Upgrade header for WebSocket? Knowing this will tell us if the observed delay is due to forced polling or another issue.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.