- Introduction
- Quick start
- Philosophy
- Comparison
- Default behaviors
- Limitations
- Debugging runbook
- FAQ
- Mocking HTTP
- Mocking GraphQL
- Mocking WebSocket
- Integrations
- API
- CLI
- Best practices
- Recipes
Bindings
To provide a more familiar experience when mocking third-party WebSocket clients, MSW uses bindings. A binding is a wrapper over the standard WebSocket
class that encapsulates the third-party-specific behaviors, such as message parsing, and gives you a public API similar to that of the bound third-party library.
For example, here’s how to handle Socket.IO communication using MSW and a designated SocketIO binding:
import { ws } from 'msw' import { toSocketIo } from '@mswjs/socket.io-binding' const chat = ws.link('wss://chat.example.com') export const handlers = [ chat.addEventListener('connection', (connection) => { const io = toSocketIo(connection) io.client.on('hello', (username) => { io.client.emit('message', `hello, ${username}!`) }) }), ]
@mswjs/socket.io-binding
Connection wrapper for mocking Socket.IO with MSW.
Note that binding is not meant to cover all the public APIs of the respective third-party library. Unless the binding is shipped by that library, maintaining full compatibility is not feasible.