WebSockets (WIP)
WebSockets is a modern web technology that enables a persistent, two-way ("full-duplex") communication channel between a client (like your browser) and a server.
Unlike normal HTTP requests—where the browser always initiates the connection and waits for a reply—a WebSocket connection allows messages to be sent both from the client to the server and from the server back to the client at any time, over the same connection.
With WebSockets, after an initial handshake using HTTP, the protocol "upgrades" the connection so both client and server can continuously exchange data in real time.
This all happens over a single TCP connection (the same connection is kept open for as long as needed).
What’s the benefit compared to XMLHttpRequest?
When you use traditional web methods like
XMLHttpRequest
(as in AJAX), every message you want to send opens a new connection (or at least a new request/response cycle). This can add delay and overhead.With WebSockets, the same open connection is reused, so data can flow rapidly back and forth without repeatedly reconnecting or waiting for each response before sending the next request.
Why does this matter?
This makes WebSockets ideal for real-time applications such as chats, live sports, online games, financial tickers, or collaborative editing, where quick updates from server to client are crucial.
Summary
WebSockets:
Full-duplex (two-way) communication
Uses a single, long-lived TCP connection
Faster and more efficient for real-time interactions
XMLHttpRequest (AJAX):
Opens a new request/response cycle each time
Less efficient for real-time updates
Last updated