Long polling is a technique where the client sends a request to the server and the server holds the request open until new data is available or a timeout occurs. Then the server responds, and the client immediately sends another request to wait for more data.
Click to reveal answer
beginner
Why use long polling as a fallback method?
Long polling is used as a fallback when real-time communication methods like WebSockets are not supported by the client or network. It simulates real-time updates by repeatedly requesting data from the server.
Click to reveal answer
intermediate
How does long polling differ from regular polling?
Regular polling sends requests at fixed intervals regardless of data availability, which can waste resources. Long polling keeps the request open until data is ready, reducing unnecessary requests and improving efficiency.
Click to reveal answer
intermediate
In Node.js, what is a key consideration when implementing long polling?
You must manage open connections carefully to avoid blocking the event loop. Using asynchronous callbacks and proper timeout handling ensures the server remains responsive while holding requests open.
Click to reveal answer
beginner
What happens on the client side after receiving a response from a long polling request?
The client processes the received data and immediately sends a new long polling request to the server to wait for the next update, maintaining a continuous connection-like experience.
Click to reveal answer
What does long polling do differently compared to regular polling?
AKeeps the request open until data is available or timeout
BSends requests at fixed intervals regardless of data
CUses WebSocket protocol for communication
DCloses connection immediately after sending request
✗ Incorrect
Long polling holds the request open until new data is ready or a timeout occurs, unlike regular polling which sends requests at fixed intervals.
Why might long polling be used as a fallback method?
ABecause it requires no client requests
BBecause it is faster than WebSockets
CBecause it uses less server memory than WebSockets
DBecause WebSockets are not supported or blocked
✗ Incorrect
Long polling is used when WebSockets are not supported or blocked by the client or network.
In Node.js, what should you avoid when implementing long polling?
ABlocking the event loop with synchronous code
BUsing asynchronous callbacks
CSetting timeouts for requests
DSending multiple requests from the client
✗ Incorrect
Blocking the event loop with synchronous code can make the server unresponsive during long polling.
What does the client do after receiving data from a long polling response?
AWaits for a fixed time before sending another request
BCloses the connection and stops requests
CSends a new long polling request immediately
DSwitches to WebSocket communication
✗ Incorrect
The client immediately sends a new long polling request to keep waiting for updates.
Which of these is NOT a benefit of long polling?
AReduces unnecessary requests compared to regular polling
BUses less bandwidth than WebSockets
CSimulates real-time updates without WebSockets
DWorks in environments where WebSockets are blocked
✗ Incorrect
Long polling generally uses more bandwidth than WebSockets because it repeatedly opens and closes HTTP connections.
Explain how long polling works as a fallback method for real-time communication.
Think about how the client and server keep talking without WebSockets.
You got /5 concepts.
Describe key considerations when implementing long polling in Node.js.
Focus on how Node.js handles many requests at once.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using long polling in Node.js applications?
easy
A. To keep a client request open until new data is available
B. To immediately close the connection after sending data
C. To send multiple responses to a single request
D. To replace WebSocket connections entirely
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 A
Quick Check:
Long polling = open request until data ready [OK]
Hint: Long polling holds request open until data arrives [OK]
Common Mistakes:
Thinking long polling sends multiple responses per request
Confusing long polling with WebSocket persistent connection
Assuming long polling closes connection immediately
2. Which of the following is the correct way to implement a long polling endpoint in Node.js using Express?
easy
A. res.write('data'); // keep connection open without res.end()
B. res.send('data'); res.end();
C. res.json('data'); res.end();
D. res.send('data'); // then wait before closing connection
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 A
Quick Check:
Keep connection open = no res.end() [OK]
Hint: Use res.write() without res.end() to keep connection open [OK]
Common Mistakes:
Calling res.end() immediately closes connection
Using res.send() closes connection right away
Not sending any response causes timeout
3. Consider this Node.js long polling code snippet:
5. You want to implement long polling as a fallback in a Node.js app that first tries WebSocket. Which approach correctly handles fallback and client reconnection?
hard
A. Use WebSocket and long polling simultaneously, sending duplicate data to client
B. Use WebSocket only; if unavailable, do nothing and drop updates
C. Use long polling only; never try WebSocket to simplify code
D. Use WebSocket; if unavailable, client repeatedly sends GET requests that wait on server until data or timeout, then reconnect
Solution
Step 1: Understand fallback strategy
WebSocket is preferred for real-time, but fallback uses long polling with repeated client requests.