Promissa saysThe WebSocket client opens a persistent two-way connection for real-time data.
const ws = new WebSocket("wss://…") upgrades to a live socket. Handle onopen, onmessage, onclose, onerror; send with ws.send(data). Unlike fetch (request/response), the server can push at any time. Use wss:// on HTTPS pages, and add your own reconnect logic — it needs a WebSocket server, so it can't run in this offline sandbox.
Power-ups you unlock
new WebSocket(url) → persistent two-way link
onopen/onmessage/onclose/onerror; ws.send()
Server can push anytime (not request/response)
Use wss:// on https; handle reconnect yourself
Race Condition Rex attacks — common mistakes
ws:// on an https page (blocked — use wss://)
No reconnect on dropped connections
Sending before onopen fires
Boss battleSketch a WebSocket client: open, send a hello, log messages, reconnect on close.
Example code
<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre>const ws = new WebSocket('wss://echo.websocket.events');
ws.onopen = () => ws.send('hello');
ws.onmessage = e => console.log('←', e.data);
ws.onclose = () => {/* reconnect here */};</pre></body></html>