Advanced Topic 43 of 50 Computer Networks
WebSockets
Persistent full-duplex channels between browser and server.
How it works
A WebSocket provides a persistent, full-duplex connection between a browser or client and a server over a single TCP connection. It starts as an HTTP request with an Upgrade header, after which both sides can send small framed messages at any time without repeated request overhead. Frames can carry text or binary data, with ping and pong frames for keep-alive and close frames to end the connection.
Step-by-step walkthrough
- The browser sends an HTTP request with Upgrade: websocket.
- The server agrees with 101 Switching Protocols.
- The connection stays open.
- Either side can now send messages at any time.
- It suits chat, live prices and games.
Where it is used
Chat applications, live dashboards, collaborative editing, multiplayer games, stock tickers and notifications use WebSockets.
Common pitfall
Long-lived connections consume server resources and complicate load balancing and scaling, since state is tied to a connection. Proxies and firewalls may drop idle connections, so use heartbeats and reconnection with backoff. Authenticate during the handshake and validate origins to prevent cross-site hijacking. For one-way updates, Server-Sent Events may be simpler.
Where it fits in the course
WebSockets is topic 43 of 50 in Computer Networks, rated Advanced. It follows QUIC and HTTP/3. The next topic is Software-Defined Networking.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Computer Networks quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Computer Networks quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is WebSockets?
Where do people use WebSockets?
What is the most common mistake with WebSockets?
What should I learn before this topic?
How can I practice it?
Is WebSockets hard to learn?
How long does it take to learn WebSockets?
Which topics are related to WebSockets?
What should I study after WebSockets?
Related topics
- QUIC and HTTP/3 (Computer Networks)
- Software-Defined Networking (Computer Networks)
- VPN (Computer Networks)
- Network Virtualization (Computer Networks)