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
$ yarn add fastify-uws
$ pnpm i fastify-uws
$ bun add fastify-uws
Usage
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;
};
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();
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;
import multipart from '@fastify/multipart';
app.register(multipart);
import type { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox';
export default (async (app) => {
app.post('', async (req, reply) => {
const data = await req.file();
data.file;
data.fields;
data.fieldname;
data.filename;
data.encoding;
data.mimetype;
return reply.send({ message: 'ok' });
});
}) as FastifyPluginAsyncTypebox;
import { websocket } from 'fastify-uws';
app.register(websocket);
import type { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox';
export default (async (app) => {
app.get('', { websocket: true }, (con) => {
console.log('Client connected');
con.socket.send('Hello from Fastify uWS!');
con.socket.on('message', (message) => {
console.log(`Client message: ${message.toString()}`);
});
con.socket.on('close', () => {
console.log('Client disconnected');
});
});
}) as FastifyPluginAsyncTypebox;
import { eventsource } from 'fastify-uws';
app.register(eventsource);
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);
});
}) as FastifyPluginAsyncTypebox;
Benchmarks
$ oha -c 100 -z 30s http://127.0.0.1:3000
| Version | Requests/sec |
---|
uws | 20.42.0 | 186,296.26 |
bun | 1.0.28 | 172,787.09 |
deno | 1.41.0 | 127,581.76 |
fastify-uws | 0.5.0 | 91,977.19 |
node | 20.11.1 | 75,519.58 |
fastify (bun) | 4.26.1 | 73,320.81 |
fastify | 4.26.1 | 70,916.76 |
fastify (deno) | 4.26.1 | 62,583.69 |
$ bombardier -c 100 -d 30s http://127.0.0.1:3000
| Version | Requests/sec |
---|
uws | 20.42.0 | 203,021.23 |
bun | 1.0.28 | 198,411.55 |
deno | 1.41.0 | 136,654.51 |
fastify-uws | 0.5.0 | 110,131.81 |
node | 20.11.1 | 86,161.47 |
fastify (bun) | 4.26.1 | 83,639.41 |
fastify | 4.26.1 | 78,550.22 |
fastify (deno) | 4.26.1 | 70,608.36 |