@fastify/websocket
Adds WebSocket support, letting routes handle upgraded connections.
Syntax
app.register(websocket); app.get(url, { websocket: true }, handler) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
options | object | No | Optional options forwarded to the ws server. |
Returns
Promise<void> — Resolves when the plugin is registered.
Examples
import Fastify from 'fastify';
import websocket from '@fastify/websocket';
const app = Fastify();
await app.register(websocket);
app.get('/chat', { websocket: true }, (socket, req) => {
socket.on('message', (msg) => {
socket.send(`echo: ${msg}`);
});
});
Notes
Mark a route with `{ websocket: true }`; the handler receives the socket
and the request. Regular HTTP handlers and WebSocket handlers can share
the same URL.