The dictionary meaning of “poll” as a verb is as follows – check the status of (a device), especially as part of a repeated cycle.
In computers and networking, polling is an activity where the status of something (a device/resource) is checked periodically. In a client-server architecture, polling is a way in which the client requests for the status of a resource from the server at regular intervals.
In older architectures, this was a common pattern where the client pulls data from the server periodically and updates its view accordingly. Based on how frequently is this done, we have two types of polling –
- Short Polling (or Polling)
- Long Polling
Before looking at each of these, let’s understand what happens when a client makes a request to the server. Many things happen, but the below are most notable ones –
- make a connection to the server
- prepare data as packets to send
- write the packets onto the connection channel
- server side – read the packets from the connection
- server side – read the request headers
- server side – process the information, write result to the same channel
- close the connection
Whenever a client makes a (web) request to the server, all these things happen.
Short Polling (or Polling) is a pattern, in which the client makes asynchronous requests to the server at very short intervals, in the hope of receiving latest information. For example, the current trajectory of a satellite to be plotted over a time-graph.
In Short Polling –
- Client makes a connection for a request
- Server may or may not have a message to send – so if nothing then times out
- Client then closes connection
- Delay (say 10s)
- Repeat from step 1
But in the hurry to get as live information as possible, the client ends up making too many requests (very short intervals) and all the above mentioned steps need to be repeated every time. This takes up a lot of resources (computing and network) and may end up in clogging the network.
Long Polling on the other hand, works a bit different. In this case, the client makes a connection to the server with a request and keeps the channel open, until the server returns with some new data to the client.
The connection is kept open for a “long” time, and the server uses the same channel to write to the client. Once the data is written, the server closes the connection.
In Long Polling –
- Client makes a connection, and hangs on
- Server – when has something to write after sometime, puts the message and closes the connection
This way many of the steps above are not repeated (mainly steps 1 and 7) and thus makes too less requests.
But on the other hand, the server and the client needs to spend some of their resources in keeping the channel open, till some data is written and doesn’t make any sense, if there are many messages to be written.
In modern day real-time communications, we have more sophisticated techniques – Web Sockets, Server Sent Events and frameworks that makes implementing these easy.
That’s all about Polling, I hope you have got to know something interesting.
Please share it with your friends if you find it interesting.