6.0.0 • Published 9 months ago

@geut/fastify-uws v6.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

uWebSockets.js for fastify

fastify-uws

uWebSockets.js for fastify

Tests JavaScript Style Guide standard-readme compliant

Made by GEUT

Install

$ npm install @geut/fastify-uws

Usage

import { getUws, serverFactory, WebSocketStream } from '@geut/fastify-uws'
import fastifyUwsPlugin from '@geut/fastify-uws/plugin'
import fastify from 'fastify'

const app = fastify({
  serverFactory
})

await app.register(fastifyUwsPlugin)

app.addHook('onReady', async () => {
  // access to uws app
  const uwsApp = getUws(app)
})

app.websocketServer.on('open', (ws) => {
  console.log('OPEN')
})

app.websocketServer.on('close', (ws) => {
  console.log('CLOSE')
})

app
  .route({
    method: 'GET',
    url: '/',
    handler(req, reply) {
      return 'hello from http endpoint'
    },
    uws: {
      // cache subscription topics to produce less memory allocations
      topics: [
        'home/sensors/ligth',
        'home/sensors/temp'
      ]
    },
    uwsHandler(conn) {
      conn.subscribe('home/sensors/temp')
      conn.on('message', (message) => {
        conn.publish('home/sensors/temp', 'random message')
      })
      conn.send(JSON.stringify({ hello: 'world' }))
    }
  })
  .get('/stream', { uws: true }, (conn) => {
    const stream = new WebSocketStream(conn)
    stream.on('data', (data) => {
      console.log('stream data from /stream')
    })
  })
  .listen({
    port: 3000
  }, (err) => {
    err && console.error(err)
  })

Benchmarks

  • Machine: linux x64 | 2 vCPUs | 6.8GB Mem
  • Node: v18.15.0
  • Run: Wed Apr 12 2023 19:06:58 GMT+0000 (Coordinated Universal Time)
  • Method: autocannon -c 100 -d 40 -p 10 localhost:3000 (two rounds; one to warm-up, one to measure)
VersionRouterRequests/sLatency (ms)Throughput/Mb
fastify-uws1.0.084001.611.5012.58
0httpv3.5.150275.219.418.97
bare10.13.049063.219.948.75
h31.6.448583.220.107.97
fastify4.15.048141.620.308.63
h3-router1.6.448025.620.347.88
polka0.5.247676.020.498.50
server-base7.1.3247286.420.688.43
server-base-router7.1.3246884.020.858.36
yeps1.1.145748.021.368.16
connect3.7.045615.221.448.14
connect-router1.3.844720.021.917.97
vapr0.6.043516.022.487.14
spirit0.6.143287.222.647.72
spirit-router0.5.041488.023.647.40
polkadot1.0.039672.824.737.07
koa2.14.238013.425.806.78
yeps-router1.2.036993.826.546.60
take-five2.0.036582.226.8613.15
koa-isomorphic-router1.0.136292.627.076.47
restify11.1.035689.027.536.43
koa-router12.0.033882.229.036.04
restana4.9.733645.429.266.00
hapi21.3.132087.230.685.72
express4.18.211337.087.642.02
fastify-big-json4.15.011012.290.32126.70
express-with-middlewares4.18.210000.899.453.72
trpc-router10.19.16594.1150.951.97
foxify0.10.20N/AN/AN/A
galatajs0.1.1N/AN/AN/A
micro-route2.5.0N/AN/AN/A
micro10.0.1N/AN/AN/A
microrouter3.1.3N/AN/AN/A
total.js3.4.13N/AN/AN/A

Issues

:bug: If you found an issue we encourage you to report it on github. Please specify your OS and the actions to reproduce it.

Contributing

:busts_in_silhouette: Ideas and contributions to the project are welcome. You must follow this guideline.

License

MIT © A GEUT project

6.0.0

9 months ago

5.2.0

10 months ago

5.1.0

10 months ago

5.0.1

1 year ago

5.0.0

1 year ago

4.0.0

1 year ago

3.1.1

1 year ago

3.1.0

1 year ago

3.0.0

1 year ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.2

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.0.9

3 years ago

0.0.8

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