Long-Polling vs WebSockets vs Server-Sent Events Flashcards
What are long-polling, websocket, and server-sent events?
Long-Polling, WebSockets, and Server-Sent Events are popular communication protocols between a client like a web browser and a web server.
What is ajax polling, and why is its primary disadvantage?
Ajax is async requests to a server (don’t have to reload web page). Polling is when ajax:
- The client opens a connection and requests data from the server using regular HTTP.
- The requested webpage sends requests to the server at regular intervals (e.g., 0.5 seconds).
- The server calculates the response and sends it back, just like regular HTTP traffic.
- The client repeats the above three steps periodically to get updates from the server.
The problem with Polling is that the client has to keep asking the server for any new data. As a result, a lot of responses are empty, creating HTTP overhead.
What is long-polling?
With Long-Polling, the client requests information from the server exactly as in normal polling, but with the expectation that the server may not respond immediately. That’s why this technique is sometimes referred to as a “Hanging GET”.
What is the basic life cycle of an application using HTTP long-polling?
The client sends a request to the server.
The server holds the request open until it has new data (or a timeout).
When new data is ready, the server responds and closes the connection.
The client immediately sends another request to start the process again.
Each Long-Poll request has a timeout. The client has to reconnect periodically after the connection is closed due to timeouts.
What is a full-duplex system?
A full-duplex (FDX) system allows communication in both directions, and, unlike half-duplex, allows this to happen simultaneously
What is websockts?
WebSocket provides Full duplex communication channels over a single TCP connection. It provides a persistent connection between a client and a server that both parties can use to start sending data at any time.
What is an SSE?
A server sent event.
How does an SSE work?
Aclient establishes a persistent and long-term connection with the server. The server uses this connection to send data to a client. If the client wants to send data to the server, it would require the use of another technology/protocol to do so.
1.Client requests data from a server using regular HTTP.
2. The requested webpage opens a connection to the server.
3. The server sends the data to the client whenever there’s new information available.
When are SSE’s best?
SSEs are best when we need real-time traffic from the server to the client or if the server is generating data in a loop and will be sending multiple events to the client.
How is SSE different than long-polling?
SSE is a browser-native way to stream updates from the server to the client over a single HTTP connection.
Long-polling has a similar effect, but its mechanism of action requires waiting for a server response, with a timeout, and re-requesting once a response is given.
What’s a TL;DR comparisong between SSE’s, long-polling, and web-sockets?
WebSockets: 🔄 Two-way, real-time, low-latency — best for interactive apps.
SSE: 📥 Simple, efficient one-way streaming from server to client.
Long Polling: 🧻 Works everywhere, but inefficient. Use only when the others aren’t an option.