0
0
Expressframework~20 mins

Why real-time matters in Express - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Real-Time Express Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a real-time Express server receives a new message?

Consider an Express server using WebSocket for real-time chat. When a client sends a new message, what does the server do to update all connected clients immediately?

Express
const express = require('express');
const http = require('http');
const WebSocket = require('ws');

const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });

wss.on('connection', ws => {
  ws.on('message', message => {
    // What should happen here?
  });
});

server.listen(3000);
ABroadcast the received message to all connected clients immediately.
BStore the message in a database and wait for clients to request updates.
CSend an email notification to clients about the new message.
DIgnore the message and log it only on the server.
Attempts:
2 left
💡 Hint

Think about how real-time communication works to keep all users updated instantly.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Express real-time setup

Look at the following Express and WebSocket server code. Which option correctly identifies the syntax error?

Express
const express = require('express');
const http = require('http');
const WebSocket = require('ws');

const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });

wss.on('connection', ws => {
  ws.on('message', message => {
    wss.clients.forEach(client => {
      if (client.readyState === WebSocket.OPEN) {
        client.send(message)
      }
    });
  });
});

server.listen(3000);
AMissing parentheses in server.listen call.
BIncorrect use of arrow function syntax in wss.on('connection').
CMissing semicolon after client.send(message).
DWebSocket.Server should be called with 'app' instead of 'server'.
Attempts:
2 left
💡 Hint

Check each line carefully for punctuation errors.

state_output
advanced
2:00remaining
What is the state of connected clients after broadcasting a message?

In an Express WebSocket server, after broadcasting a message to all clients, what is the state of the clients that received the message?

Express
wss.clients.forEach(client => {
  if (client.readyState === WebSocket.OPEN) {
    client.send('Hello');
  }
});
AClients enter a paused state until they acknowledge the message.
BClients throw an error if they receive a message without a callback.
CClients disconnect automatically after receiving the message.
DAll clients remain connected and have received the message.
Attempts:
2 left
💡 Hint

Think about how WebSocket connections behave during message sending.

🔧 Debug
advanced
2:00remaining
Why does this Express WebSocket server fail to broadcast messages?

Given this code, why do clients not receive broadcast messages?

Express
const express = require('express');
const http = require('http');
const WebSocket = require('ws');

const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });

wss.on('connection', ws => {
  ws.on('message', message => {
    wss.clients.forEach(client => {
      if (client.readyState === WebSocket.CLOSED) {
        client.send(message);
      }
    });
  });
});

server.listen(3000);
AThe condition checks for WebSocket.CLOSED instead of WebSocket.OPEN, so no clients receive messages.
BThe 'message' event is not properly handled with a callback function.
CThe server is not listening on the correct port.
DWebSocket.Server is not correctly initialized with the HTTP server.
Attempts:
2 left
💡 Hint

Check the condition inside the forEach loop carefully.

🧠 Conceptual
expert
2:00remaining
Why is real-time communication critical in Express apps?

Which of the following best explains why real-time communication is important in Express applications?

AIt reduces server load by batching all client requests into one response per minute.
BIt allows the server to push updates instantly to clients, improving user experience in apps like chat or live notifications.
CIt ensures clients only receive data after refreshing the page manually.
DIt replaces the need for any database by storing all data in memory.
Attempts:
2 left
💡 Hint

Think about how apps like chat or live sports updates work.