Short Polling vs Long Polling
Tags: webdev, node, javascript, architecture
Published: 2021-05-28 14:28:12

Hello everyone👋

Before proceeding, I am assuming, you are aware of the request-response architecture of a basic web application. To build a real-time application like a chat application we can't use the basic request-response architecture, but we can use it with a `polling` mechanism to achieve the real-time behaviour. Apart from the polling mechanism we also have `SSE(server-side event)` and `WebSocket` to achieve real-time behaviour.

In this article, I am going to discuss two types of polling techniques i.e, `long polling` and `short polling`.

First, we need to know, what is `polling`?

### Polling

Polling is a technique in which the client sends a request to the server asking for data in an interval of time.

> The response from the server can be empty or any kind of data.

### 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** sends 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.

This technique is very simple and doesn't consume server resources, but event notifications are not so instant and there will be some delay.

#### Example

A client sends a request to the server asking for data, but the data is not available at that time and the server responds with an empty response.

The client will wait for 5 seconds before sending the next request. That means if, in that interval of time (5 seconds), any data is available the client will not be notified about this. 

The client will be able to get the data when it sends the next request.

So this technique is not so instant, and there will some delay.

### 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.

---

> Originally published on [blog.bibekkakati.me](https://blog.bibekkakati.me/short-polling-vs-long-polling)

---

Thank you for reading 🙏

If you enjoyed this article or found it helpful, give it a thumbs-up 👍

Feel free to connect 👋

[Twitter](https://twitter.com/kakatibibek) | [Instagram](https://instagram.com/bibekkakati) | [LinkedIn](https://linkedin.com/in/bibekkakati)


---

If you like my work and want to support it, you can do it here. I will really appreciate it.

<a href="https://www.buymeacoffee.com/bibekkakati" target="_blank">
<img src="https://img.buymeacoffee.com/button-api/?text=Buy me a coffee&emoji=&slug=bibekkakati&button_colour=FFDD00&font_colour=000000&font_family=Cookie&outline_colour=000000&coffee_colour=ffffff">
</a>