Real-time WebSocket server with rooms, broadcasting, and reconnection handling.
import { WebSocketServer } from 'ws';
const wss = new WebSocketServer({ port: 8080 });
const rooms = new Map();
wss.on('connection', (ws) => {
ws.isAlive = true;
ws.on('pong', () => { ws.isAlive = true; });
ws.on('message', (data) => {
const msg = JSON.parse(data);
if (msg.type === 'join') joinRoom(ws, msg.room);
if (msg.type === 'chat') broadcast(msg.room, msg);
});
});Open in RoadCode