2.0.0 • Published 2 years ago

@kakang/fastify-ws v2.0.0

Weekly downloads
-
License
GPL-3.0
Repository
github
Last release
2 years ago

@kakang/fastify-ws

Continuous Integration Package Manager CI NPM version GitHub package.json version Coverage Status GitHub

This package is a fork of fastify-websocket and rewritten as Typescript. It provides more utility for using the websocket, e.g. boardcast.

Install

npm install @kakang/fastify-ws --save

yarn add @kakang/fastify-ws

Usages

After regitered the plugin, you can pass { ws: true } to the route option. By default, it accepts the websocket handler when using the shorten method.

import Fastify from 'fastify'
import { fastifyWS } from '@kakang/fastify-ws'

fastify.register(fastifyWS)
fastify.get('/', { ws: true }, (request /* WebsocketFastifyRequest */) => {
  const { socket } = request.ws
  socket.on('message', message => {
    // message.toString() === 'hi from client'
    socket.send('hi from server')
  })
})

WebsocketFastifyRequest

WebsocketFastifyRequest is extended from FastifyRequest and provide some utility for websocket usage.

WebsocketFastifyRequest#subscribe

Subscribe to a topic.

import Fastify from 'fastify'
import { fastifyWS } from '@kakang/fastify-ws'

fastify.register(fastifyWS)
fastify.get('/', { ws: true }, (request /* WebsocketFastifyRequest */) => {
  const { subscribe } = request.ws
  subscribe('foo')
})

WebsocketFastifyRequest#unsubscribe

Unsubscribe from a topic.

import Fastify from 'fastify'
import { fastifyWS } from '@kakang/fastify-ws'

fastify.register(fastifyWS)
fastify.get('/', { ws: true }, (request /* WebsocketFastifyRequest */) => {
  const { unsubscribe } = request.ws
  unsubscribe('foo')
})

WebsocketFastifyRequest#boardcast

Boardcast to all websocket except itself.

import Fastify from 'fastify'
import { fastifyWS } from '@kakang/fastify-ws'

fastify.register(fastifyWS)
fastify.get('/', { ws: true }, (request /* WebsocketFastifyRequest */) => {
  const { boardcast } = request.ws
  boardcast('hello all.')
})

FastifyInstance

This plugin also decorate the fastify instance to provide more ability for normal route communicate to the websocket.

FastifyInstance#server

ws Server instance.

import Fastify from 'fastify'
import { fastifyWS } from '@kakang/fastify-ws'

fastify.register(fastifyWS)
fastify.ws.server // ws instance

FastifyInstance#clients

All avaiable websocket clients. It is a shortcut of fastify.ws.server.clients

import Fastify from 'fastify'
import { fastifyWS } from '@kakang/fastify-ws'

fastify.register(fastifyWS)
fastify.ws.clients // websocket clients
fastify.ws.server.clients = fastify.ws.clients // true

FastifyInstance#topics

All topics that have registered before.

import Fastify from 'fastify'
import { fastifyWS } from '@kakang/fastify-ws'

fastify.register(fastifyWS)
for(const topic of fastify.ws.topics) {
  console.log(topic)
}

FastifyInstance#topicMap

All topic with WebsocketFastifyRequest.

import Fastify from 'fastify'
import { fastifyWS } from '@kakang/fastify-ws'

fastify.register(fastifyWS)
for(const [topic, requests] of fastify.ws.topicMap.entries()) {
  for(const request of requests) {
    console.log(topic, request)
  }
}

FastifyInstance#boardcast

Boardcast to all websocket.

import Fastify from 'fastify'
import { fastifyWS } from '@kakang/fastify-ws'

fastify.register(fastifyWS)
fastify.ws.boardcast('hello all.')

FastifyInstance#boardcastToTopic

Boardcast to all websocket in specified topic.

import Fastify from 'fastify'
import { fastifyWS } from '@kakang/fastify-ws'

fastify.register(fastifyWS)
fastify.ws.boardcastToTopic('foo', 'hello foo.')
2.0.0

2 years ago

1.0.5

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.0.12

3 years ago

0.0.13

3 years ago

0.0.11

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago