Skip to content

trufflehq/fastify-uws

This branch is 5 commits ahead of, 24 commits behind geut/fastify-uws:main.

Folders and files

NameName
Last commit message
Last commit date
Apr 12, 2023
Feb 1, 2024
Nov 5, 2023
Feb 1, 2024
Nov 5, 2023
Nov 5, 2023
Oct 2, 2022
Oct 2, 2022
Nov 14, 2023
Oct 2, 2022
Apr 12, 2023
Oct 2, 2022
Oct 2, 2022
Nov 5, 2023
Nov 5, 2023
Nov 5, 2023
Nov 5, 2023

Repository files navigation

fastify-uws

uWebSockets.js for fastify

Tests JavaScript Style Guide standard-readme compliant

Made by GEUT

Install

$ npm install @geut/fastify-uws

Usage

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

import fastifyUwsPlugin from '@geut/fastify-uws/plugin'

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)
Version Router Requests/s Latency (ms) Throughput/Mb
fastify-uws 1.0.0 84001.6 11.50 12.58
0http v3.5.1 50275.2 19.41 8.97
bare 10.13.0 49063.2 19.94 8.75
h3 1.6.4 48583.2 20.10 7.97
fastify 4.15.0 48141.6 20.30 8.63
h3-router 1.6.4 48025.6 20.34 7.88
polka 0.5.2 47676.0 20.49 8.50
server-base 7.1.32 47286.4 20.68 8.43
server-base-router 7.1.32 46884.0 20.85 8.36
yeps 1.1.1 45748.0 21.36 8.16
connect 3.7.0 45615.2 21.44 8.14
connect-router 1.3.8 44720.0 21.91 7.97
vapr 0.6.0 43516.0 22.48 7.14
spirit 0.6.1 43287.2 22.64 7.72
spirit-router 0.5.0 41488.0 23.64 7.40
polkadot 1.0.0 39672.8 24.73 7.07
koa 2.14.2 38013.4 25.80 6.78
yeps-router 1.2.0 36993.8 26.54 6.60
take-five 2.0.0 36582.2 26.86 13.15
koa-isomorphic-router 1.0.1 36292.6 27.07 6.47
restify 11.1.0 35689.0 27.53 6.43
koa-router 12.0.0 33882.2 29.03 6.04
restana 4.9.7 33645.4 29.26 6.00
hapi 21.3.1 32087.2 30.68 5.72
express 4.18.2 11337.0 87.64 2.02
fastify-big-json 4.15.0 11012.2 90.32 126.70
express-with-middlewares 4.18.2 10000.8 99.45 3.72
trpc-router 10.19.1 6594.1 150.95 1.97
foxify 0.10.20 N/A N/A N/A
galatajs 0.1.1 N/A N/A N/A
micro-route 2.5.0 N/A N/A N/A
micro 10.0.1 N/A N/A N/A
microrouter 3.1.3 N/A N/A N/A
total.js 3.4.13 N/A N/A N/A

Issues

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

Contributing

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

License

MIT © A GEUT project

About

uWebSocket.js for fastify

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%