TL;DR
Streaming and polling are two different techniques used to handle data transfer between a client and a server.
Polling → repeatedly requesting data or information from a device or service.
Polling = if data is updated on a intervals. Example: stock prices, weather reports.
Streaming → refers to the continuous flow of data or information over time.
Streaming = if instant update is needed. Example: messaging apps, live video streaming.
Keep in mind that streaming and polling are not mutually exclusive, and sometimes both can be used together in a system. For example, a real-time chat application might use streaming to push new chat messages to connected clients, while also using polling to check for new friend requests or unread messages.
Other types:
- Server-Sent Events = it's only for sending events from the server to the client.
- WebHooks = sending a message if some event occurred. Used in event-driven systems. Faster than pooling, because one app should ask another app if there is a new data.
- WebSockets = a protocol for bidirectional, real-time communication between a client and a server over a single, long-lived connection.
Polling
Polling is a technique where the client repeatedly sends requests to the server to check for new data.
The server responds with the requested data if it's available, or with an empty response if there is no new data. This can be useful for handling smaller amounts of data that change infrequently, such as stock prices or weather reports.
Types:
- Pull polling = standard
- Long polling = request is open until new data is arrived on server
- Adaptive polling = dynamically changing FREQUENCY RATE based on rate of change of data
In pull polling, the client initiates a request to the server to retrieve new data. This is the most common type of polling, where the client repeatedly sends requests to the server to check for new data.
This is a variation of the polling technique, where the client sends a request to the server and the server holds the request open until new data is available, or a set amount of time has elapsed. This allows the server to push new data to the client as soon as it's available, without the need for the client to continuously send requests.
Adaptive polling is a strategy that adjusts the polling frequency based on the rate of change of the data. The client dynamically adapts the polling rate by increasing or decreasing the polling frequency depending on the rate of change of the data.
Limitations:
- What if we want instantly receiving a message? Example: Telegram messages. What if we set 0.1 second polling? But, it is bad because we make sending lots of requests, and loading a server. 5 clients every 0.1 seconds = 5 clients * (1 second / 0.1 seconds per request) = 50 requests per second. Here a streaming is coming.
Streaming
Streaming is a technique where data is transferred in real-time, as it becomes available. The server sends data to the client as soon as it's available, allowing the client to start processing it right away.
In streaming, clients continuously receive data without issuing many requests. Instead of waiting the request, server is pushing the data proactively.
Example: telegram chats, HTTP Live Streaming (HLS) for video streaming where the server sends small chunks of video instead of sending the entire video file.
Types:
- Live streaming: broadcasting event in real-time
- On-demand streaming: stream pre-recorded videos, audios
- Adaptive streaming: adapting the quality of the video or audio stream based on the client's network conditions or device specifications
- Peer-to-peer streaming: P2P network, clients can share files among other clients
- WebRTC: gaming, conferencing
P2P streaming is a method of video streaming where clients can share video files among themselves, as well as downloading from a central server. This allows clients to share the streaming load, reducing the load on the central server, and increasing the scalability of the system.