0
0
Expressframework~20 mins

Setting up WebSocket server in Express - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
WebSocket Server Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this WebSocket server code output when a client connects?

Consider this Express server setup with WebSocket. What message does the server send to the client upon connection?

Express
import express from 'express';
import { WebSocketServer } from 'ws';

const app = express();
const server = app.listen(3000);
const wss = new WebSocketServer({ server });

wss.on('connection', ws => {
  ws.send('Welcome client!');
});
AThe client receives 'Welcome client!' message immediately after connecting.
BThe server closes the connection immediately after client connects.
CThe server throws an error because WebSocketServer is not properly initialized.
DThe client receives no message until it sends one first.
Attempts:
2 left
💡 Hint

Look at the ws.send inside the connection event handler.

📝 Syntax
intermediate
2:00remaining
Which option correctly initializes a WebSocket server with Express?

Choose the correct way to create a WebSocket server that shares the same HTTP server as Express.

Aconst wss = new WebSocketServer({ server: app.listen(3000) });
Bconst wss = new WebSocketServer({ port: 3000 });
Cconst wss = new WebSocketServer({ expressApp: app });
Dconst wss = new WebSocketServer({ httpServer: app });
Attempts:
2 left
💡 Hint

The WebSocket server needs the actual HTTP server instance, not the Express app.

🔧 Debug
advanced
2:00remaining
Why does this WebSocket server code cause a runtime error?

Identify the cause of the error in this code snippet:

Express
import express from 'express';
import { WebSocketServer } from 'ws';

const app = express();
const wss = new WebSocketServer({ server: app });

wss.on('connection', ws => {
  ws.send('Hello');
});

app.listen(3000);
AThe WebSocketServer is missing the port number, so it cannot listen.
BThe ws.send method is called before the connection is established.
CThe 'connection' event is misspelled, so the handler never runs.
DThe WebSocketServer is given the Express app instead of the HTTP server, causing a type error.
Attempts:
2 left
💡 Hint

Check what server option expects in WebSocketServer constructor.

state_output
advanced
2:00remaining
What is the value of 'count' after 3 clients connect to this WebSocket server?

Consider this code that counts connected clients:

Express
import express from 'express';
import { WebSocketServer } from 'ws';

const app = express();
const server = app.listen(3000);
const wss = new WebSocketServer({ server });

let count = 0;

wss.on('connection', ws => {
  count++;
  ws.on('close', () => {
    count--;
  });
});
A1
B0
C3
DCannot determine without client disconnections
Attempts:
2 left
💡 Hint

Each new connection increments count by 1.

🧠 Conceptual
expert
3:00remaining
Which option best explains why WebSocketServer needs the HTTP server instance from Express?

Why must the WebSocket server share the HTTP server instance created by Express?

ABecause WebSocketServer only works with HTTPS servers, and Express creates them automatically.
BBecause WebSocketServer upgrades HTTP connections to WebSocket using the same server, enabling both protocols on one port.
CBecause WebSocketServer requires Express middleware to function properly.
DBecause WebSocketServer cannot listen on any port by itself and needs Express to handle routing.
Attempts:
2 left
💡 Hint

Think about how WebSocket connections start as HTTP requests.