Implementing Fallback Strategies

1. Detecting WebSocket Support

Example: Capability check

const hasWS = typeof WebSocket !== "undefined";
const transport = hasWS ? "ws" : "longpoll";
CheckDetail
API existstypeof WebSocket
Successful connectProbe handshake
Sustained openMaintains for > N sec
Proxy passthroughEcho round-trip works

2. Implementing Long Polling Fallback

StepDetail
Client GET /poll?cursor=NServer holds until event
Server responseReturns events + new cursor
Client re-pollsImmediately with new cursor
POST /sendOutbound separately

3. Implementing SSE Fallback

Example: EventSource

const es = new EventSource("/sse?token=" + jwt);
es.onmessage = (e) => handle(JSON.parse(e.data));
es.addEventListener("notification", (e) => toast(e.data));
AspectSSE
DirectionServer → Client only
Auto-reconnectBuilt-in
OutboundHTTP POST separately
ProxyPure HTTP, traverses easily

4. Using Socket.IO Transports

TransportUse
websocketDefault if available
pollingHTTP long-poll fallback
webtransportHTTP/3 (Socket.IO v4.7+)

Example: Socket.IO client

import { io } from "socket.io-client";
const s = io("https://api.example.com", { transports: ["websocket","polling"] });
s.on("connect", () => console.log("via", s.io.engine.transport.name));

5. Implementing Graceful Degradation

CapabilityFallback
BidirectionalSSE + POST
Binarybase64 over text
Low latencyAggressive polling
Reliable orderServer-assigned seq

6. Handling Mixed Connections

ConcernDetail
Unified APIAdapter pattern hides transport
Feature parityLowest-common-denominator API
MetricsTag by transport

7. Testing Fallback Mechanisms

TestHow
Force fallbackBlock ws in proxy
Toggle mid-sessionNetwork mode change
Behavior paritySame test suite both transports

8. Implementing Progressive Enhancement

TierUse
No JSPlain HTML / Refresh
JS + fetchPoll periodically
JS + SSEStream from server
JS + WSFull bidirectional

9. Communicating Fallback Status

UI HintDetail
"Limited connection"Polling mode active
Reduced featuresDisable typing indicators, etc.
Reconnect promptManual retry button

10. Monitoring Fallback Usage

MetricUse
% conns on fallbackNetwork-quality signal
Fallback by regionIdentify problematic ISPs
Upgrade success rateWS handshake stats