Discover how to make your app feel instantly alive without crashing your server!
Why Long polling as fallback in Node.js? - Purpose & Use Cases
Imagine building a chat app where the browser asks the server every second if there are new messages by sending repeated requests.
This constant asking floods the server with requests, wastes bandwidth, and makes the app slow and unreliable when many users connect.
Long polling lets the server hold the request open until new data is ready, so the client gets updates instantly without asking repeatedly.
setInterval(() => fetch('/messages'), 1000);
function longPoll() { fetch('/messages').then(() => longPoll()); } longPoll();This approach enables real-time updates with fewer requests, saving resources and improving user experience.
In a live sports score app, long polling lets users see score changes immediately without refreshing or spamming the server.
Manual repeated requests overload servers and slow apps.
Long polling waits for new data before responding.
It creates efficient, real-time communication as a fallback.
Practice
Solution
Step 1: Understand long polling behavior
Long polling keeps the client request open until the server has new data to send.Step 2: Compare options to this behavior
Only To keep a client request open until new data is available describes this behavior correctly; others describe different or incorrect behaviors.Final Answer:
To keep a client request open until new data is available -> Option AQuick Check:
Long polling = open request until data ready [OK]
- Thinking long polling sends multiple responses per request
- Confusing long polling with WebSocket persistent connection
- Assuming long polling closes connection immediately
Solution
Step 1: Identify how to keep connection open
In long polling, the server holds the connection open, so it should not call res.end() immediately.Step 2: Check which option keeps connection open
res.write('data'); // keep connection open without res.end() uses res.write() without res.end(), which keeps the connection open for long polling.Final Answer:
res.write('data'); // keep connection open without res.end() -> Option AQuick Check:
Keep connection open = no res.end() [OK]
- Calling res.end() immediately closes connection
- Using res.send() closes connection right away
- Not sending any response causes timeout
app.get('/poll', (req, res) => {
setTimeout(() => {
res.json({ message: 'Update available' });
}, 5000);
});
What will the client experience when calling this endpoint?Solution
Step 1: Analyze the setTimeout delay
The server waits 5 seconds before sending the JSON response.Step 2: Understand client wait behavior
The client request stays open during the 5 seconds, then receives the update once sent.Final Answer:
The client waits 5 seconds, then receives the update -> Option CQuick Check:
setTimeout delay = delayed response [OK]
- Assuming immediate response despite delay
- Expecting multiple responses from one request
- Thinking client never gets response
app.get('/poll', (req, res) => {
res.json({ message: 'Update' });
setTimeout(() => {
res.json({ message: 'Another update' });
}, 3000);
});Solution
Step 1: Understand res.json() behavior
res.json() sends a response and closes the connection immediately.Step 2: Analyze calling res.json() twice
Calling res.json() a second time after the connection is closed causes an error.Final Answer:
Calling res.json() twice causes an error -> Option BQuick Check:
Multiple res.json() calls = error [OK]
- Thinking setTimeout blocks code
- Assuming multiple res.json() calls send multiple responses
- Ignoring connection close after first response
Solution
Step 1: Understand fallback strategy
WebSocket is preferred for real-time, but fallback uses long polling with repeated client requests.Step 2: Identify correct client reconnection method
The client must send repeated GET requests that wait on server until data or timeout, then reconnect to keep updates flowing.Final Answer:
Use WebSocket; if unavailable, client repeatedly sends GET requests that wait on server until data or timeout, then reconnect -> Option DQuick Check:
Fallback = repeated long polling requests [OK]
- Ignoring fallback and dropping updates
- Sending duplicate data by using both methods simultaneously
- Not reconnecting client after long polling timeout
Ready to prove it?
Pass the test and this pattern moves unknown
