Right Choice ( Request-Response || Polling || Long-Polling || SSE || WebSockets )

Which model should you choose?

Keshav Singh
TheLeanProgrammer
4 min readApr 15, 2021

--

In this blog, I am going to share something important that everyone should consider discussing with their team before building any Application whether it be a Stock Market Application, Real-Time Chat Application, Blogging Website, etc.

This is going to be really exciting. Let’s dive right in!

Developing an Application is not just choosing a Technology Stack like MERN, MEAN, etc. during development, we should also consider other factors which are usually related to the cost involved in the development. For example, the number of network calls involved, choosing the right architecture like Monolith, Microservice, etc.

This post deals with one of the factors that need to be focused on i.e Network calls. It plays an important role in the application’s success or failure.

Let's see it through an example before moving around terms like Request-Response, Polling, Long-Polling, SSE, WebSockets.

The best example to consider is Chat Application. Suppose you are developing a chat application, you want the application to have features like one-to-one chat and group chat. So, when someone sends a message on a group chat, everyone involved in the chat should see the message posted. There are many ways to implement this.

  1. Chat Client checks for a new message at a regular interval from the server.
  2. Chat Client maintains a duplex connection. (real-time)
  3. Chat Server pushes messages to all clients whenever a new message comes.
  4. Chat Client opens a connection until a response comes from the server. This cycle repeats.
  5. Chat Client requests for new messages from Chat Server.

After going through this example, you might have got some idea of what my post focuses on. Now, let’s understand all terms mentioned.

What is the Request-Response method?

It is one of the traditional ways of sending data over the web. In simpler terms, we can say that it is a kind of one-way communication. Here, the client always starts a conversation and the server responds to that. The server is never gonna start a conversation.

This method is suitable for applications like:

  • Blogging App ( like Medium, Blogspot, etc)
  • Online Coding Platform ( like Codechef, Codeforces, etc)

What is Polling?

In this, the client makes a request at a regular interval specified asking for updates. It is similar to Request-Response but here the client is making a request at intervals and sometimes response can be empty. Here, the client can start an unnecessary conversation.

Not suitable for Real-time chat Applications.

This method is suitable for applications like:

  • Applications requiring Interval Updates ( like 1 min, 5 min, 1 hour, etc)

What is Long-Polling?

It is just a variation of Polling. In this method, the client waits for a response by opening a connection with a timeout. When it gets a response, it again waits for another response. So, it avoids unnecessary conversation.

This method can be used for applications like:

  • Chat Application ( Not Optimal )
  • Some Cloud Platform Services ( like Monitoring )

What is SSE?

SSE stands for Server- Side Events. Developing a web application that uses server-sent events is straightforward. You’ll need a bit of code on the server to stream events to the front-end, but the client-side code works almost identically to WebSockets(will see later) in part of handling incoming events. This is a one-way connection, so you can’t send events from a client to a server.

This method is suitable for applications like:

  • Stock Market Application ( Used by FinnHub, AlphaVantage, etc stock API providers)
  • Application involving Push Notifications

What is WebSockets?

A WebSocket is a persistent connection between a client and server. WebSockets provide a bidirectional, full-duplex communications channel that operates over HTTP through a single TCP/IP socket connection. Here, both the client and server can start a conversation. Channel allows them to communicate in real-time.

This method is suitable for applications like:

  • Real-time Chat Application( like Whatsapp)
  • Gaming Applications
  • Stock-Market Application(for more accuracy)

Now, since you all have a better understanding of these terms, you can make the Right Choice for your Application. In production, this choice really affects the cost of projects on cloud services like GCP, Azure, and AWS, etc.

This is my first post here. I hope that this added some value to and further, I am really excited to see your response. This was more a theoretical post. In the future, I will be posting coding tutorials related to technologies that are used in Industry.

Stay tuned for more amazing posts. You can find me on LinkedIn here.

Don’t forget to follow The Lean Programmer Publication for more such articles, and subscribe to our newsletter tinyletter.com/TheLeanProgrammer

--

--

Keshav Singh
TheLeanProgrammer

SDE @Amazon Madrid | Former SDE Intern @Samsung R&D || NITK Grad