WebSockets and Server-Sent Events
WebSockets and Server-Sent Events keep a browser connected to a server so updates can arrive without repeated polling. WebSockets carry messages in both directions, while Server-Sent Events stream text events from server to browser over HTTP.
itWeb development | OpenSkills.info
Course pathWalk it in order
Look it upDip in anytime
Go furtherLeaves this page
Don't Panic
Don't Panic: WebSockets and Server-Sent Events
WebSockets and Server-Sent Events are what happens when a browser page stops asking, "Has anything changed yet?" every few seconds. Both keep a connection open so updates can arrive when the server has them. The useful distinction is pleasantly unromantic: a WebSocket is a two-way message channel, while SSE is an HTTP response that keeps sending text events from server to browser.
Direction does most of the choosing. A build log, dashboard, notification feed, or generated-text stream usually has one author, the server, so SSE fits without inventing an upstream message channel. Collaborative editing, a terminal, a game, or signaling has two independent talkers, so WebSocket earns its extra machinery. When updates are rare, polling remains a respectable answer. A permanent connection is not a loyalty oath.
Reconnection is the bit that tends to arrive wearing a reassuring hat and then leave the application holding the bill. EventSource reconnects, and a WebSocket client can reconnect, but neither action restores the state that existed before the break. An event identifier is a bookmark, not a time machine. Important streams need a snapshot or retained history, sequence rules, and duplicate handling before they can claim recovery.
Long-lived connections also turn capacity into a different kind of arithmetic. Requests per second still matter, but so do concurrent connections, memory, file descriptors, proxy timeouts, outbound bandwidth, and the number of clients that decide to reconnect together. TLS protects the link. It does not decide who may subscribe, what a message means, or whether a slow client should stay connected forever. Those are application decisions, which is slightly rude of them, but also useful to know early.
Read the intro for the transport shapes and security boundaries. Use the slides when you need the comparison in one pass, and the cheatsheet when a field name, lifecycle state, or proxy check has escaped at an inconvenient moment. The practice reference and exercise turn the SSE format into a local stream. Field Notes is where the operational costs stop being theoretical.
Where this skill leads
Relevant careers
See how this topic contributes to broader role-level skill maps.
Sources
- https://www.rfc-editor.org/rfc/rfc6455
Supports
- WebSocket purpose, HTTP/1.1 opening handshake, status 101, and two-way message channel
- Text, binary, fragmented, close, ping, and pong frame behavior
- Client masking, server non-masking, Origin validation, subprotocols, and TLS guidance
- December 2011 publication milestone
- https://html.spec.whatwg.org/multipage/web-sockets.html
Supports
- Browser WebSocket constructor, events, ready states, send, close, and bufferedAmount
- https://html.spec.whatwg.org/multipage/server-sent-events.html
Supports
- EventSource interface, automatic reconnection, Last-Event-ID, and close behavior
- text/event-stream UTF-8 parsing, data, event, id, retry, comments, and blank-line dispatch
- SSE as a server-to-browser transport
- https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
Supports
- Practical browser API reference and absence of incoming backpressure in the classic WebSocket interface
- https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
Supports
- Browser and server implementation path for EventSource, named events, errors, and event streams
- https://www.rfc-editor.org/rfc/rfc7692
Supports
- WebSocket extension negotiation and permessage-deflate compression
- December 2015 publication milestone
- https://www.rfc-editor.org/rfc/rfc7936
Supports
- July 2016 clarification of the WebSocket subprotocol registry procedure
- https://www.rfc-editor.org/rfc/rfc8307
Supports
- January 2018 well-known URI suffix registration for ws and wss
- https://www.rfc-editor.org/rfc/rfc8441
Supports
- Extended CONNECT bootstrapping of WebSocket on an HTTP/2 stream
- September 2018 publication milestone
- https://www.rfc-editor.org/rfc/rfc9220
Supports
- WebSocket bootstrapping over an HTTP/3 stream
- June 2022 publication milestone
- https://www.w3.org/standards/history/websockets/
Supports
- WebSocket API Candidate Recommendation publication history
- https://www.w3.org/news/2011/w3c-invites-implementations-of-the-websocket-api-web-storage/
Supports
- December 8 2011 invitation for WebSocket API implementations
- https://www.w3.org/standards/history/eventsource/
Supports
- Server-Sent Events milestones from April 2009 Working Draft through December 2012 Candidate Recommendation and February 2015 Recommendation
- https://www.w3.org/wiki/Webapps/Interop/ServerSentEvents
Supports
- Implementation report for the December 2012 Server-Sent Events Candidate Recommendation
- https://www.w3.org/news/2015/server-sent-events-is-a-w3c-recommendation/
Supports
- February 2015 Server-Sent Events Recommendation and its scope
- https://github.com/sindresorhus/awesome
Supports
- Required starting catalog for awesome-list discovery
- https://github.com/facundofarias/awesome-websockets
Supports
- Discovery of Socket.IO, Centrifugo, SignalR, websocat, wscat, and libwebsockets as WebSocket ecosystem projects
- https://socket.io/docs/v4/
Supports
- Socket.IO transport fallback, reconnection, acknowledgments, rooms, and connection-state recovery
- Socket.IO placement in Awesome Links and Landscape
- https://centrifugal.dev/docs/getting-started/introduction
Supports
- Centrifugo channel fan-out, history, recovery, presence, WebSocket, HTTP streaming, and SSE transports
- Centrifugo placement in Awesome Links and Landscape
- https://learn.microsoft.com/en-us/aspnet/core/signalr/introduction
Supports
- SignalR hubs, message protocols, and fallback order across WebSockets, SSE, and long polling
- https://github.com/vi/websocat
Supports
- websocat command-line client, server, proxy, text, and binary capabilities
- https://github.com/websockets/wscat
Supports
- wscat interactive WebSocket client and server capabilities
- https://libwebsockets.org/
Supports
- libwebsockets role as a C client and server library for resource-conscious deployments
- https://ably.com/docs/protocols/sse
Supports
- Ably standards-based SSE adapter, subscribe-only behavior, and EventSource use
- Ably Pub/Sub placement in the managed realtime market
- https://pusher.com/docs/channels/
Supports
- Pusher publish-subscribe channels, WebSocket and HTTP transports, private channels, and presence
- https://www.pubnub.com/docs/general/channels/overview
Supports
- PubNub hosted channels and publish-subscribe messaging model
- https://learn.microsoft.com/en-us/azure/azure-web-pubsub/
Supports
- Azure-managed WebSocket connections, publish-subscribe hubs, groups, client protocols, and reliability guidance
- https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api.html
Supports
- API Gateway bidirectional WebSocket routes and integration with HTTP endpoints, Lambda, and AWS services
- https://developers.cloudflare.com/durable-objects/best-practices/websockets/
Supports
- Durable Objects as WebSocket servers, per-instance client coordination, and hibernation behavior
- https://blog.cloudflare.com/how-to-stop-running-out-of-ephemeral-ports-and-start-to-love-long-lived-connections/
Supports
- Operational effects of long-lived WebSocket proxy connections on concurrent outbound connection and ephemeral-port capacity
- Failure signals and diagnosis for ephemeral port exhaustion caused by outbound connection pressure
- https://blog.postman.com/postman-engineering-million-concurrent-connections/
Supports
- Postman engineering account of reconnect cascades, deployment-induced WebSocket disconnects, and separating connection handling from application services
- https://blog.cloudflare.com/ecdysis-rust-graceful-restarts/
Supports
- Operational consequences of restarting services that carry long-lived WebSocket connections and the value of connection-aware graceful restart design
- https://nodejs.org/api/http.html
Supports
- Node.js HTTP server creation, response headers, and incremental response writes used by the local SSE practice
- https://developer.mozilla.org/en-US/docs/Web/API/EventSource/EventSource
Supports
- Browser EventSource construction and message handling used by the local SSE practice
