GithubHelp home page GithubHelp logo

geut / fastify-uws Goto Github PK

View Code? Open in Web Editor NEW
32.0 5.0 2.0 1.82 MB

uWebSocket.js for fastify

License: MIT License

JavaScript 100.00%
fastify fastify-plugin uwebsockets websocket uwebsocketjs nodejs uws

fastify-uws's Introduction

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

fastify-uws's People

Contributors

tinchoz49 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

fastify-uws's Issues

bug with using credentials

A bug occurs when using a POST request with credentials and passing data through the request.body. If credentials not using or if turn off serverFactory, so all works is correctly. How to fix this?

Can get uWsApp object?

Thanks for the great library and the example.

I just wonder how to publish websocket server itself rather than rely on client's reply?

I added "App" property to websocketServer, it seems work, but I want to know there is better way.

In addition, can we still use UserData or getUserData to identify client? or there is alternative way to distinguish user? Since it does upgrade out-of-box unlike to uws, It is bit confusing though it is very helpful.

addServer(server) {
    const { options } = this;
    const app = (this.App = server[kApp]);
    const listenerHandler = server[kHandler];

    app.ws("/*", {
      compression: options.compression,
      idleTimeout: options.idleTimeout,
...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.