0
0
Node.jsframework~10 mins

Long polling as fallback in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Long polling as fallback
Client sends request
Server holds request open
Event occurs or timeout
Server sends response
Client receives data
Client immediately sends new request
Back to Client sends request
The client sends a request that the server keeps open until new data is ready or a timeout happens. Then the server responds, and the client immediately sends another request to keep listening.
Execution Sample
Node.js
app.get('/poll', (req, res) => {
  holdRequestUntilEventOrTimeout(() => {
    res.json({ data: 'new data' });
  });
});
Server holds the GET /poll request until new data is ready or timeout, then responds with JSON.
Execution Table
StepClient ActionServer StateServer ResponseClient Reaction
1Sends GET /pollWaiting for event or timeoutNo response yetWaiting
2WaitingEvent occursSends JSON dataReceives data
3Sends new GET /poll immediatelyWaiting for event or timeoutNo response yetWaiting
4WaitingTimeout reached (no event)Sends empty JSONReceives empty data
5Sends new GET /poll immediatelyWaiting for event or timeoutNo response yetWaiting
6WaitingEvent occursSends JSON dataReceives data
7Stops sending requestsNo request receivedNo responseStops polling
💡 Client stops sending requests, so server no longer holds connections.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
ClientRequestNoneGET /poll sentGET /poll sentGET /poll sentNone (stopped)
ServerHoldNoYes (holding)Yes (holding)Yes (holding)No
ServerResponseNoneJSON data sentEmpty JSON sentJSON data sentNone
ClientDataNoneReceived dataReceived empty dataReceived dataNone
Key Moments - 3 Insights
Why does the client send a new request immediately after receiving data?
Because long polling keeps the connection open only until data is sent or timeout. To keep listening for new data, the client must send a new request as shown in steps 2 and 3 of the execution_table.
What happens if no event occurs before the timeout?
The server sends an empty or timeout response (step 4), so the client knows to send a new request to keep polling, preventing the connection from hanging indefinitely.
Why does the server hold the request open instead of responding immediately?
Holding the request lets the server send data only when new information is available, reducing unnecessary responses and simulating real-time updates as shown in the server state during steps 1 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the server state at Step 3?
ANo request received
BWaiting for event or timeout
CSending JSON data
DTimeout reached
💡 Hint
Check the 'Server State' column for Step 3 in the execution_table.
At which step does the client receive empty data due to timeout?
AStep 4
BStep 2
CStep 6
DStep 7
💡 Hint
Look at the 'Server Response' and 'Client Reaction' columns in the execution_table.
If the client stops sending new requests, what happens to the server state?
AServer keeps holding requests
BServer sends continuous responses
CServer has no requests to hold
DServer crashes
💡 Hint
Refer to the 'exit_note' and final rows in the execution_table.
Concept Snapshot
Long polling keeps a client request open until new data or timeout.
Server responds only when ready or timed out.
Client immediately sends a new request after response.
This simulates real-time updates without WebSockets.
Useful as fallback when WebSockets are unavailable.
Full Transcript
Long polling is a technique where the client sends a request to the server, and the server holds this request open until new data is available or a timeout occurs. When the server responds, the client immediately sends another request to keep listening for updates. This cycle continues until the client stops polling. This method helps simulate real-time communication without needing WebSockets. The server state changes from waiting to responding, and the client reacts by sending new requests or stopping. If no event occurs before timeout, the server sends an empty response, prompting the client to send a new request. This ensures the connection does not hang indefinitely.