How can you create a WebSocket server in Node.js?

WebSockets provide a full-duplex communication channel over a single TCP connection. You can create a WebSocket server using the ws library:

const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', (ws) => {
  ws.on('message', (message) => {
    console.log(`Received: ${message}`);
  });
  ws.send('Welcome to the WebSocket server!');
});