You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Routes registered with `fastify-websocket` respect the Fastify plugin encapsulation contexts, and so will run any hooks that have been registered. This means the same route hooks you might use for authentication or error handling of plain old HTTP handlers will apply to websocket handlers as well.
// the connection will only be opened for authenticated incoming requests
117
+
connection.socket.on('message', message=> {
118
+
// ...
119
+
})
120
+
})
121
+
```
104
122
105
123
**NB**
106
124
This plugin uses the same router as the `fastify` instance, this has a few implications to take into account:
107
-
- Websocket route handlers follow the usual `fastify` request lifecycle.
125
+
- Websocket route handlers follow the usual `fastify` request lifecycle, which means hooks, error handlers, and decorators all work the same way as other route handlers.
108
126
- You can access the fastify server via `this` in your handlers
109
-
- You can access the fastify request decorations via the `req` object your handlers
110
127
- When using `fastify-websocket`, it needs to be registered before all routes in order to be able to intercept websocket connections to existing routes and close the connection on non-websocket routes.
0 commit comments