Complete the code to import the WebSocket library.
const WebSocket = require('[1]');
The WebSocket server library is called ws. We import it using require('ws').
Complete the code to create a WebSocket server on top of an existing HTTP server.
const wss = new WebSocket.Server({ [1] });To attach a WebSocket server to an existing HTTP server, use the server option with the HTTP server instance.
Fix the error in the WebSocket connection event handler to correctly receive the client socket.
wss.on('connection', function([1]) { console.log('Client connected'); });
The event handler receives the client socket as the first argument, commonly named socket.
Fill both blanks to send a message to the client when a connection is established.
wss.on('connection', function(socket) { socket.[1]('[2]'); });
Use the send method on the client socket to send a message string.
Fill all three blanks to handle incoming messages and log them.
wss.on('connection', function(socket) { socket.on('[1]', function([2]) { console.log('Received:', [3]); }); });
The event to listen for incoming messages is message. The callback receives the message data, commonly named data.