fastify-websocket-server
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

fastify-websocket-server

Fastify plugin that creates websocket server on your fastify instance.

NPM Version Downloads Count Vunerabilities Count Build Status Coverage Status License

ToC

Why not use fastify-ws?

  • 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

Dependents (0)

Package Sidebar

Install

npm i fastify-websocket-server

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

9.41 kB

Total Files

8

Last publish

Collaborators

  • m03geek