- Introduction
- Quick start
- Philosophy
- Comparison
- Default behaviors
- Limitations
- Debugging runbook
- FAQ
- Mocking HTTP
- Mocking SSE
- Mocking GraphQL
- Mocking WebSocket
- Integrations
- API
- CLI
- Best practices
- Recipes
Sending data
Sending mocked data to a single WebSocket client.
You can send data to the intercepted WebSocket client by calling client.send() anywhere in the connection event listener:
api.addEventListener('connection', ({ client }) => {
client.send('hello from server!')
})You can send text,
Blob, andArrayBufferto the client.
This will send the given data to the particular client, and it will receive and handle it as if it was received from the server. For sending data to multiple clients, see broadcasting data.
Examples
Sending text
api.addEventListener('connection', ({ client }) => {
client.send('hello world')
})Sending Blob
api.addEventListener('connection', ({ client }) => {
client.send(new Blob(['hello world'], { type: 'text/plain' }))
})Sending ArrayBuffer
api.addEventListener('connection', ({ client }) => {
client.send(new TextEncoder().encode('hello world'))
})