Socket
Socket
Sign inDemoInstall

fastify-uws

Package Overview
Dependencies
22
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    fastify-uws

A performant HTTP and WebSocket server for Fastify with uWebSockets.


Version published
Maintainers
1
Created

Readme

Source

fastify-uws

A performant HTTP and WebSocket server for Fastify with uWebSockets.

Installation

Install fastify-uws with your favorite package manager:

$ npm i fastify-uws
# or
$ yarn add fastify-uws
# or
$ pnpm i fastify-uws
# or
$ bun add fastify-uws

Usage

// app.ts
import fastify from 'fastify';
import { serverFactory } from 'fastify-uws';

import router from '~/plugins/router';

export default () => {
  const app = fastify({
    logger: {
      transport: {
        target: '@fastify/one-line-logger',
      },
    },
    serverFactory,
  });

  app.register(router);

  return app;
};
// server.ts
import app from './app';

const server = app();

const start = async () => {
  try {
    await server.listen({
      host: '127.0.0.1',
      port: 3000,
    });
  } catch (err) {
    server.log.error(err);
    process.exit(1);
  }
};

start();

Use Fetch

// src/routes/hello-http/+handler.ts
import type { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox';
import { Type } from '@sinclair/typebox';

export default (async (app) => {
  app.get(
    '',
    {
      schema: {
        response: {
          200: Type.Object({
            message: Type.String(),
          }),
        },
      },
    },
    async (req, reply) => {
      return reply.send({
        message: 'Hello, World!',
      });
    },
  );
}) as FastifyPluginAsyncTypebox;
With FormData
// app.ts
import multipart from '@fastify/multipart';

app.register(multipart);
// src/routes/hello-fd/+handler.ts
import type { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox';

export default (async (app) => {
  app.post('', async (req, reply) => {
    const data = await req.file();

    data.file; // stream
    data.fields; // other parsed parts
    data.fieldname;
    data.filename;
    data.encoding;
    data.mimetype;

    // await data.toBuffer(); // Buffer

    return reply.send({ message: 'ok' });
  });
}) as FastifyPluginAsyncTypebox;

Use WebSocket

// app.ts
import { websocket } from 'fastify-uws';

app.register(websocket);
// src/routes/hello-ws/+handler.ts
import type { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox';

export default (async (app) => {
  app.get('', { websocket: true }, (socket, request) => {
    app.log.info('Client connected');

    socket.on('message', (message: MessageEvent) => {
      console.log(`Client message: ${message}`);
      socket.send('Hello from Fastify!');
    });

    socket.on('close', () => {
      app.log.info('Client disconnected');
    });
  });
}) as FastifyPluginAsyncTypebox;

Use EventSource

// app.ts
import { eventsource } from 'fastify-uws';

app.register(eventsource);
// src/routes/hello-sse/+handler.ts
import type { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox';

export default (async (app) => {
  app.get('', (req, reply) => {
    let index = 0;

    reply.sse({ id: String(index), data: `Some message ${index}` });

    const interval = setInterval(() => {
      index += 1;

      reply.sse({ id: String(index), data: `Some message ${index}` });

      if (index === 10) {
        clearInterval(interval);
      }
    }, 1000);

    req.raw.on('close', () => {
      clearInterval(interval);
      app.log.info('Client disconnected');
      reply.sse({ event: 'close' });
    });
  });
}) as FastifyPluginAsyncTypebox;

Benchmarks

oha v1.4.1

$ oha -c 500 -z 10s --no-tui http://0.0.0.0:3000/api/hello-world
Round 1
VersionLanguageRouterRequests/sec
hyper1.2.0Rust56,262.8828
warp0.3.6Rust55,413.2098
uws20.43.0JavaScript/Node54,936.6504
viz0.8.3Rust54,544.0379
axum0.7.4Rust54,366.0321
poem2.0.1Rust54,139.7879
graphul1.0.1Rust53,958.5545
salvo0.66.2Rust53,562.5958
bun1.1.1TypeScript/Bun53,249.7334
rocket0.5.0Rust51,447.6432
elysia1.0.13TypeScript/Bun51,272.3293
actix-web4.5.1Rust49,768.7938
hono4.2.2TypeScript/Bun45,713.3907
hyper-express6.15.1JavaScript/Node45,570.4958
hono4.2.2TypeScript/Deno44,769.4540
deno1.42.1TypeScript/Deno44,662.8648
drash3.0.0-beta.2TypeScript/Deno41,604.3191
fastify-uws0.7.0JavaScript/Node37,991.9966
h31.11.1TypeScript/Deno35,210.1163
h31.11.1TypeScript/Bun32,071.3073
oak14.2.0TypeScript/Deno31,410.6134
h31.11.1JavaScript/Node30,153.7447
node20.12.1JavaScript/Node29,388.2349
fastify4.26.2JavaScript/Node27,589.0118
hono4.2.2JavaScript/Node25,198.2960
polka1.0.0-next.25JavaScript/Node22,806.9399
oak14.2.0TypeScript/Bun22,151.3947
Round 2
VersionLanguageRouterRequests/sec
warp0.3.6Rust55,053.0475
uws20.43.0JavaScript/Node54,761.6827
viz0.8.3Rust54,401.4776
axum0.7.4Rust54,395.5151
graphul1.0.1Rust54,156.1952
hyper1.2.0Rust54,141.1060
poem2.0.1Rust53,955.1222
salvo0.66.2Rust53,904.0594
bun1.1.1TypeScript/Bun51,817.2410
elysia1.0.13TypeScript/Bun51,488.6109
rocket0.5.0Rust50,618.1340
actix-web4.5.1Rust49,864.0242
hyper-express6.15.1JavaScript/Node46,558.9299
hono4.2.2TypeScript/Bun45,054.8384
deno1.42.1TypeScript/Deno44,804.4318
hono4.2.2TypeScript/Deno44,786.7461
drash3.0.0-beta.2TypeScript/Deno40,621.7867
fastify-uws0.7.0JavaScript/Node38,167.7099
h31.11.1TypeScript/Bun33,891.8063
h31.11.1TypeScript/Deno33,736.5124
oak14.2.0TypeScript/Deno31,224.4031
polka1.0.0-next.25JavaScript/Node28,623.6484
node20.12.1JavaScript/Node28,059.7339
h31.11.1JavaScript/Node27,522.4966
fastify4.26.2JavaScript/Node27,028.7908
hono4.2.2JavaScript/Node25,257.8000
oak14.2.0TypeScript/Bun22,796.7969
Round 3
VersionLanguageRouterRequests/sec
hyper1.2.0Rust55,896.3816
viz0.8.3Rust55,055.4270
salvo0.66.2Rust54,761.9791
warp0.3.6Rust54,657.2309
uws20.43.0JavaScript/Node54,305.0799
graphul1.0.1Rust54,139.9006
axum0.7.4Rust54,121.9253
poem2.0.1Rust53,843.4990
rocket0.5.0Rust51,748.7393
actix-web4.5.1Rust50,019.9149
elysia1.0.13TypeScript/Bun49,460.9121
hyper-express6.15.1JavaScript/Node47,962.9897
hono4.2.2TypeScript/Deno44,552.3948
deno1.42.1TypeScript/Deno43,262.0878
bun1.1.1TypeScript/Bun42,808.5557
hono4.2.2TypeScript/Bun42,694.7743
drash3.0.0-beta.2TypeScript/Deno41,907.4852
fastify-uws0.7.0JavaScript/Node35,952.0719
h31.11.1TypeScript/Deno34,462.8275
h31.11.1TypeScript/Bun34,348.0887
oak14.2.0TypeScript/Deno31,447.9778
h31.11.1JavaScript/Node31,340.3190
polka1.0.0-next.25JavaScript/Node29,869.0525
node20.12.1JavaScript/Node28,047.6443
fastify4.26.2JavaScript/Node26,931.3162
oak14.2.0TypeScript/Bun26,284.5028
hono4.2.2JavaScript/Node25,364.4139

Keywords

FAQs

Last updated on 10 Apr 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc