let resp = await fetch('https://<url>', { headers: { Upgrade: 'websocket', }, }); let ws = resp.webSocket; if (!ws) { throw new Error("server didn't accept WebSocket"); } ws.accept();