new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Playwright · API reference · all subjects

websocketroute

7 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

WebSocketRoute class introduction and availability

WebSocketRoute is a class available since v1.48 that allows handling WebSocket connections set up through Page.routeWebSocket or BrowserContext.routeWebSocket. It lets you handle WebSockets like an actual server would do, supporting both mocking and intercepting of WebSocket communication.

WebSocketRoute mocking behavior by default

By default, a routed WebSocket does not connect to the server, allowing you to mock entire communication over the WebSocket. The WebSocket opens inside the page automatically when you do not call connectToServer inside the route handler.

WebSocketRoute mocking example with JSON messages

This example demonstrates mocking a WebSocket that handles JSON messages, responding to a question request with an answer response: ```js await page.routeWebSocket('wss://example.com/ws', ws => { ws.onMessage(message => { const json = JSON.parse(message); if (json.request === 'question') ws.send(JSON.stringify({ response: 'answer' })); }); }); ```

WebSocketRoute intercepting example with message modification

This example demonstrates intercepting WebSocket messages, modifying messages sent from the page to the server while leaving server-to-page messages intact: ```js await page.routeWebSocket('/ws', ws => { const server = ws.connectToServer(); ws.onMessage(message => { if (message === 'request') server.send('request2'); else server.send(message); }); }); ```

WebSocketRoute blocking messages in both directions example

This example demonstrates blocking specific messages in both directions by setting onMessage handlers on both the page-side and server-side routes: ```js await page.routeWebSocket('/ws', ws => { const server = ws.connectToServer(); ws.onMessage(message => { if (message !== 'blocked-from-the-page') server.send(message); }); server.onMessage(message => { if (message !== 'blocked-from-the-server') ws.send(message); }); }); ```

WebSocketRoute protocols filtering example

This example demonstrates checking WebSocket subprotocols and closing the connection if an unsupported protocol is requested: ```js await page.routeWebSocket('wss://example.com/ws', ws => { if (ws.protocols().includes('chat.v2')) ws.onMessage(message => ws.send(JSON.stringify({ version: 2, echo: message }))); else ws.close({ code: 1002, reason: 'Unsupported protocol' }); }); ```

WebSocketRoute forwarding behavior when connectToServer is called

After connecting to the server with connectToServer(), all messages are forwarded between the page and the server by default. However, if you call onMessage on the original route, messages from the page to the server will not be forwarded anymore and should be handled by the onMessage handler. Similarly, calling onMessage on the server-side WebSocket will stop forwarding messages from the server to the page.

Give your agent this brain