Complete the code to import the ws library.
const WebSocket = require('[1]');
The ws library is imported using require('ws'). This allows you to create a WebSocket server.
Complete the code to create a new WebSocket server on port 8080.
const wss = new WebSocket.Server({ [1]: 8080 });The WebSocket.Server constructor takes an options object where 'port' specifies the port number to listen on.
Fix the error in the event listener to correctly handle new connections.
wss.on('[1]', function connection(ws) { ws.send('Welcome!'); });
The 'connection' event is emitted when a new client connects to the WebSocket server.
Fill both blanks to send a message to all connected clients.
wss.clients.forEach(function [1](client) { if (client.readyState === [2].OPEN) { client.send('Hello all!'); } });
Use 'forEach' to loop over clients. The constant 'WebSocket.OPEN' checks if the connection is open.
Fill all three blanks to handle incoming messages and reply back.
wss.on('connection', function(ws) { ws.on('[1]', function(message) { console.log('Received:', message.toString()); ws.[2]('Echo: ' + [3]); }); });
The 'message' event listens for incoming messages. Use ws.send() to reply. The received message is passed as the argument.