uWebSockets.js adapter for Tupik: serves an app on the
uWS HTTP server instead of node:http. Same app, same behavior (routing, hooks, access, validation, 404/405,
the 400 for an unsafe Host), more requests per second.
uWebSockets.js is not on the npm registry: it installs from its GitHub release tag, and this package takes it as a peer dependency.
npm install @tupik/uws @tupik/core uNetworking/uWebSockets.js#v20.72.0import { createApp, createModule } from '@tupik/core';
import { serveUws } from '@tupik/uws';
const app = createApp().register(
createModule().get('/', { access: 'public', handler: () => ({ server: 'uws' }) }),
);
const server = serveUws(app, { port: 3000 });
console.log('listening on', server.port);
// later: server.close();Same app (one JSON GET and one JSON POST echo route), Node 24, autocannon with 64 connections for 8 s, two rounds on the same machine (2026-12-02):
| Route | @tupik/node |
@tupik/uws |
|---|---|---|
GET /json |
~28.4k req/s, p99 4 to 5 ms | ~39.5k req/s, p99 3 ms |
POST /echo |
~12.6k req/s | ~17k req/s |
- Tupik apps get the same lightweight request as on
@tupik/node(no nativeRequest, headers copied only when read, abort signal created only when read), and a synchronousapp.handleresult is written without anawait. - Request bodies stream into the app with backpressure (the socket pauses while the app is not reading) and stop
at
bodyLimit(default 1 MiB) with a 413. - Responses whose body is known leave in one write with an exact
content-length; streams (SSE, large files) are written chunk by chunk, waiting foronWritablewhen uWS is full, and cancelled when the client leaves. ctx.req.signalaborts when the client disconnects.- Errors outside the app answer a generic 500;
onErrorreceives the error, and the default logs its name only.
| Option | Default | Purpose |
|---|---|---|
port |
0 |
Port; read the one picked by the OS from server.port |
hostname |
'127.0.0.1' |
Bind address |
bodyLimit |
1 MiB | Larger request bodies get 413 |
onError |
logs the error name | Unexpected failures (the client gets a generic 500) |
app |
uWS.App() |
An existing App() or SSLApp(), to add native uWS routes next to the Tupik app |
webSocket |
true |
upgradeWebSocket through the app on ws('/*'); { maxPayloadLength } sets the uWS ceiling (16 MiB); false turns it off |
uWS runs on one thread. On Linux it listens with SO_REUSEPORT, so start one process per core (for example with
node:cluster) and call serveUws with the same port in each.
upgradeWebSocket from @tupik/node works here with the same contract: serveUws registers ws('/*') next to
any('/*'), the upgrade request runs the whole pipeline (hooks, middleware, access, validation) before the 101,
and only a route that returned upgradeWebSocket(ctx, handlers, options) gets a socket. An anonymous client on an
authenticated route gets its 401 as plain HTTP; any other answer (403, 404, 426...) too, and the connection closes.
import { createApp, createModule } from '@tupik/core';
import { upgradeWebSocket } from '@tupik/node';
import { serveUws } from '@tupik/uws';
const chat = createModule('', { access: 'authenticated' }).get('/ws', {
handler: (ctx) =>
upgradeWebSocket(ctx, {
message: (ws, data) => ws.send(typeof data === 'string' ? 'echo: ' + data : data),
}, { origin: ['https://app.example'] }),
});
// `verify` is your own token check: it returns the principal or null.
const app = createApp({ authenticate: (ctx) => verify(ctx.req.headers.get('authorization')) }).register(chat);
serveUws(app, { port: 3000 });Handlers get the same ServerWebSocket (send, ping, close, readyState, bufferedAmount, protocol);
origin (403), protocols, maxPayload (default 1 MiB, close 1009), a throwing handler (1011) and idleTimeout
(default 120 s, close 1001, one timer per socket) behave as on @tupik/node, and server.close() closes open
sockets with 1001. Differences that come from uWS:
maxPayloadLength(optionwebSocket: { maxPayloadLength }, default 16 MiB) is one ceiling for the wholews('/*')route. Each socket'smaxPayloadis checked below it once uWS has the full message; a message above the ceiling makes uWS drop the connection (the client sees 1006,closegets 1006).closeTimeoutis not used: uWS sends the close frame, callscloseright away with the server's code and ends the TCP connection itself.- Response headers the gates set (cookies, request id) ride on the 101, as on Node. Extensions (permessage-deflate) are not negotiated, as on Node.
webSocket: falseskips thews('/*')route, to register your own uWSwsroutes onapp.
| Export | Purpose |
|---|---|
serveUws(app, options?) |
Registers the app on any('/*') and ws('/*'), listens and returns { app, port, close() } |
createUwsHandler(app, options?) |
The (res, req) => void handler, to mount on your own uWS routes |
MIT. uWebSockets.js is Apache-2.0.