0
0
Node.jsframework~20 mins

Long polling as fallback in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Long Polling Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Understanding long polling fallback behavior

Consider a Node.js server that uses long polling as a fallback when WebSocket is unavailable. What will the client receive if the server holds the request open until new data arrives?

Node.js
const http = require('http');
let dataAvailable = false;

http.createServer((req, res) => {
  if (req.url === '/poll') {
    if (dataAvailable) {
      res.end('New data');
      dataAvailable = false;
    } else {
      // Hold request open
      setTimeout(() => {
        res.end('No new data');
      }, 5000);
    }
  } else {
    res.end('Hello');
  }
}).listen(3000);
AThe client receives 'Hello' regardless of the URL requested.
BThe client always receives 'No new data' immediately without waiting.
CThe client request times out immediately with an error if dataAvailable is false.
DThe client receives 'New data' immediately if dataAvailable is true; otherwise, it waits 5 seconds and receives 'No new data'.
Attempts:
2 left
💡 Hint

Think about how the server responds based on the dataAvailable flag and the timeout.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in long polling implementation

Which option contains a syntax error that would prevent a Node.js long polling server from running?

Node.js
const http = require('http');

http.createServer((req, res) => {
  if (req.url === '/poll') {
    setTimeout(() => {
      res.end('Data');
    }, 3000);
  } else {
    res.end('OK');
  }
}).listen(3000);
AMissing semicolon after setTimeout call.
BMissing curly braces around if block.
CMissing closing parenthesis for setTimeout function.
DMissing callback function in createServer.
Attempts:
2 left
💡 Hint

Check the parentheses matching in the setTimeout call.

state_output
advanced
2:00remaining
What is the output after multiple long polling requests?

Given this server code, what will the client receive on the second poll request?

Node.js
const http = require('http');
let dataQueue = ['first', 'second'];

http.createServer((req, res) => {
  if (req.url === '/poll') {
    if (dataQueue.length > 0) {
      res.end(dataQueue.shift());
    } else {
      setTimeout(() => {
        res.end('no data');
      }, 2000);
    }
  } else {
    res.end('welcome');
  }
}).listen(3000);
AThe client receives 'second' immediately on the second request.
BThe client receives 'first' again on the second request.
CThe client waits 2 seconds and receives 'no data' on the second request.
DThe client receives 'welcome' on the second request.
Attempts:
2 left
💡 Hint

Think about how dataQueue.shift() changes the queue after each request.

🔧 Debug
advanced
2:00remaining
Why does this long polling server never respond?

Examine the code below. Why does the client never get a response?

Node.js
const http = require('http');
let hasData = false;

http.createServer((req, res) => {
  if (req.url === '/poll') {
    if (hasData) {
      res.end('Data ready');
      hasData = false;
    } else {
      // Missing response or timeout here
    }
  } else {
    res.end('Hello');
  }
}).listen(3000);
ABecause the hasData variable is never set to true.
BBecause the else block for no data does not send any response or end the request.
CBecause the server listens on the wrong port.
DBecause res.end is called twice in the if block.
Attempts:
2 left
💡 Hint

Check what happens when hasData is false.

🧠 Conceptual
expert
2:00remaining
Why use long polling as a fallback in real-time apps?

Which reason best explains why long polling is used as a fallback method for real-time communication when WebSocket is unavailable?

ALong polling allows the server to push updates by holding client requests open until new data is available, simulating real-time communication over HTTP.
BLong polling reduces server load by closing connections immediately after sending data.
CLong polling uses WebSocket protocol internally to maintain a persistent connection.
DLong polling requires the client to constantly reconnect without waiting for server data.
Attempts:
2 left
💡 Hint

Think about how long polling mimics real-time updates over traditional HTTP.