Back to Interview PrepGet interview ready with AILearn each topic the way interviewers ask it, then answer like you would in the interview.

Communication

Short Polling

Overview
Short polling: the client sends request A and gets a response, then sends request B and gets a response, at fixed intervals

Short polling is a way for a client, like a browser or app, to repeatedly ask the server: "Do you have any new data for me?" It does this after a fixed time interval, like every 2, 5 or 10 seconds. So instead of the server sending updates automatically, the client keeps checking again and again.

Simple example

You message your friend and then keep asking every minute: "Did you reply?" "Did you reply now?" "What about now?" That is basically how short polling works.

How it works

  1. Client sends a request to the server
  2. Server sends back the current data
  3. Client waits for a short time
  4. Client sends another request
  5. This keeps repeating

Example use cases

  • Showing notifications
  • Checking order status
  • Refreshing dashboard data
  • Chat apps with basic real-time updates
  • Stock or weather updates at intervals

Advantages

  • Easy to understand - one of the simplest ways to get updated data from a server.
  • Easy to implement - built with normal HTTP requests using fetch or axios.
  • Works almost everywhere - regular client-server requests work in most browsers and backend systems.
  • Good for small projects - enough for simple apps where real-time speed isn’t critical.

Disadvantages

  • Wastes requests - the client keeps sending requests even when no new data is available.
  • Increases server load - too many repeated requests put extra pressure on the server.
  • Not truly real-time - updates only arrive when the next request happens, so there can be delay.
  • Inefficient for large apps - if many users poll frequently, performance becomes a problem.

Code

index.html

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Short Polling Example</title>
  </head>
  <body>
    <h1>Short Polling Example</h1>
    <div id="data-container"></div>

    <script>
      async function getData() {
        try {
          const response = await fetch("/getData");
          const result = await response.json();
          document.getElementById("data-container").textContent = result.data;
        } catch (error) {
          console.log(error);
        }
      }

      // Ask the server again every 5 seconds
      setInterval(getData, 5000);
      getData();
    </script>
  </body>
</html>

index.js

const express = require("express");
const app = express();

let data = "Our initial data here";

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/index.html");
});

// Get the data from the server
app.get("/getData", (req, res) => {
  res.send({ data });
});

// Use PUT/POST to update in a real app - GET is just for the demo
app.get("/updateData", (req, res) => {
  data = "Updated data";
  res.send({ data });
});

app.listen(5000, () => console.log("App running on port 5000"));
You're viewing a free lesson. Sign up free to track your progress and unlock the full course.Sign up freeSign in