Introducing Topic
- Short polling
- Long Polling
- Websocket (Socket I/O)
Short polling
Short Polling
is a technique in which the client sends a request to the server asking for data at fixed delays after getting a response from the previously sent request.
- Client send a request to the Server.
- Server responds with an empty response or data, if available.
- Client will wait for the specified delay after receiving the response and continues the same request-response process again.
Example:
Long polling
Long Polling is a technique in which the client sends a request to the server asking for data, but the server doesn’t respond instantly if no data is available, rather it waits for a specific amount of time. If in that interval of time, any event happens or data become available, the server will respond with that data and in case of no events or data, the server will respond with an empty response after the specified timeout.
This technique is more complex and does consume server resources, but it can notify the client without any delay so it can give a better real-time experience.
Long Polling works differently from short polling in the following way:
- Client makes a request to the server
- Server can respond in two ways:
- If it has some new data available, it can respond right away.
- If it doesn’t have anything new data, it will keep that connection open for a period of time and when it receives new data it will respond back with updated data.
Some challenges in long-polling:
- Message ordering and delivery guarantees: Message ordering cannot be guaranteed if the same client opens multiple connections to the server.
- If the client was not able to receive the message then there will be possible message loss.
- Performance and scaling
- Device support and fallbacks
Web Sockets
WebSocket is a computer communication protocol that provides full-duplex communication channels over a single TCP connection.
The WebSocket protocol enables interaction between a client and a web server with lesser overheads, providing real-time data transfer from and to the server. WebSockets keeps the connection open, allowing messages to be passed back and forth between the client and the server. In this way, a two-way ongoing conversation can take place between the client and the server.
Some advantages of Web Sockets over long-polling
- WebSockets keeps a unique connection open while eliminating latency problems that arise with Long Polling.
- Long polling is much more resource-intensive on servers whereas WebSockets have an extremely lightweight footprint on servers.
- WebSockets pass through most firewalls without any reconfiguration.
- Good security model (origin-based security model).
Advantages of WebSocket
- It allows for two-way communication.
- Websockets allow you to send and receive data much faster than HTTP. They’re also faster than AJAX.
- Communication between origins (however, this poses security risks).
- Compatibility between platforms (web, desktop, mobile)
- HTTP has a 2000-byte overhead, but WebSocket only has a 2-byte cost.
- Long polling is replaced.
- AJAX calls can only send string data types because WebSockets are data typed.
Disadvantages of WebSocket
- There are still several proxies and transparent proxies not supporting WebSockets.
- A WebSocket server requires different optimizations than traditional Web servers, so that a dedicated platform might be required.
1 comment
Cảm ơn tác giả ,bài viết rất hữu ích ạ.