fastify-websocket-server
Fastify plugin that creates websocket server on your fastify instance.
ToC
fastify-ws
?
Why not use - It doesn't support all
ws
options in it's constructor. - It doesn't expose types and doesn't support typescript out of the box.
Getting started
Installation
npm i fastify-websocket-server
Usage
const fastify = require('fastify')()
const websocketPlugin = require('fastify-websocket-server');
fastify.register(websocketPlugin, {
// The same options that `ws` supports
}).after((err) => {
if (err) { throw err; }
fastify.wss
.on('connection', (ws) => {
ws.on('message', (msg) => {
socket.send(msg)); // echo message
}
})
})
fastify.listen(3000);
Docs
Docs available via link.
Roadmap
Any ideas or PR welcome
Support for "typed" websockets
Allow setting message types via query param or some header. This will set default message mode.
Better support for JSON messages
The idea is if websocket message type is set to JSON to add following:
- support JSON message validation against schema, just like Fastify does.
- use
fast-json-stringify
module to serialize messages with schemas