k0d13/next-ws
Acerca de
WebSockets en las rutas de aplicación de Next.js
Otros proyectosCrecimiento de estrellas medido
- in 24h
- 0
- in 7 days
- +1
- in 15 days
- +1
- in 1 month
- Recopilando historial
Cambios netos en períodos que se solapan, no totales diarios. Los datos no disponibles se dejan en blanco. Las estrellas indican interés, no calidad.
README.md
OriginalNext WS
Add support for WebSockets in Next.js app directorynpm install next-ws ws
🤔 About
next-ws is a simple package that adds WebSocket support to your Next.js app directory. With next-ws, you no longer need to create a separate WebSocket server to handle WebSocket connections. Instead, you can handle WebSocket connections directly in your Next.js API routes.
[!IMPORTANT]
Next WS is designed for use in server-based environments. It is not suitable for serverless platforms like Vercel, where WebSocket servers are not supported. Furthermore, this plugin is built for the app directory and does not support the older pages directory.
🏓 Table of Contents
📦 Installation
next-ws supports Next.js 15 and 16, on Node.js 20.9 or later.
To set up a WebSocket server with next-ws, you need to patch your local Next.js installation. next-ws simplifies this process by providing a CLI command that handles the patching for you. Follow these steps to get started:
Install Dependencies: Use your preferred package manager to install
next-wsand its peer dependencyws:npm install next-ws ws pnpm add next-ws ws yarn add next-ws wsAdd Prepare Script: Add the following
preparescript to yourpackage.json. Thepreparescript is a lifecycle script that runs automatically when you runnpm install, ensuring that your Next.js installation is patched withnext-wsevery time you install it:{ "scripts": { "prepare": "next-ws patch" } }
🚀 Usage
Handle WebSocket connections in any route file in the app directory by exporting an UPGRADE function. It runs once for each new connection.
// app/api/rooms/[room]/route.ts
import type { NextRequest } from 'next/server';
import type { WebSocket, WebSocketServer } from 'ws';
export async function UPGRADE(
client: WebSocket,
server: WebSocketServer,
request: NextRequest,
context: RouteContext<'/api/rooms/[room]'>,
) {
const { room } = await context.params;
client.on('message', (message) => client.send(`${room}: ${message}`));
}
| Argument | What it is |
|---|---|
client |
The connection, a WebSocket from ws. |
server |
The WebSocketServer every connection belongs to, server.clients lists all of them. |
request |
The upgrade request as a NextRequest, with its URL, headers and cookies. |
context |
Route parameters in context.params, such as { room: 'general' } for /rooms/[room]/ws. |
context has the same shape as for any route handler, so the global RouteContext type Next.js generates (Next.js 15.5 and later) fits it, and params is a promise to await. On older versions, import RouteContext from next-ws/server instead. For code written for next-ws 2, the parameters can still be read straight off context.params without awaiting.
UPGRADE can await before it listens for messages, such as to check a session. Messages the client sends in the meantime are held until it adds a message listener or returns, rather than lost.
Requests to a path without a matching route, or to a route without UPGRADE, are refused with a 404. With a custom server, upgrades to paths that are not routes are left for its own upgrade listeners instead. If UPGRADE throws, the connection is closed with code 1011.
Headers and cookies
headers() and cookies() from next/headers work inside UPGRADE, and inside any event listener it adds, so authentication works the way it does in a page:
import { cookies } from 'next/headers';
export async function UPGRADE(client: WebSocket) {
const session = await getSession((await cookies()).get('session')?.value);
if (!session) return client.close(1008, 'Unauthorised');
// ...
}
Rooms and sending from anywhere on the server
Put clients in rooms with join(), and send to everyone in a room with broadcast(). Clients leave every room when they close. Server actions and route handlers run in the same process as the WebSocket server, so they can broadcast too:
// app/api/rooms/[room]/route.ts
import { broadcast, join } from 'next-ws/server';
export async function UPGRADE(client: WebSocket, server: WebSocketServer, request: NextRequest) {
const room = request.nextUrl.pathname;
await join(client, room);
// Send every message on to the rest of the room
client.on('message', (data) => broadcast(room, data.toString(), { except: client }));
}
// app/actions.ts
'use server';
import { broadcast } from 'next-ws/server';
export async function announce(text: string) {
await broadcast('/api/rooms/general', text);
}
| Function | What it does |
|---|---|
join(client, room) |
Adds a client to a room. Join only once a client is allowed in. |
leave(client, room) |
Removes a client from a room. |
broadcast(room, data, { except }?) |
Sends to every client in a room, on every instance with an adapter. |
getClients(room) |
Lists the clients in a room on this instance. |
getWebSocketServer() |
The WebSocketServer itself, whose clients are every connection on any route. |
Configuring the WebSocket server
next-ws creates a WebSocketServer with the defaults of ws, which accept messages of up to 100 MiB. To change its options, set your own in instrumentation.ts, which Next.js runs before next-ws accepts any connection:
// instrumentation.ts
export async function register() {
if (process.env.NEXT_RUNTIME !== 'nodejs') return;
const { setWebSocketServer } = await import('next-ws/server');
const { WebSocketServer } = await import('ws');
setWebSocketServer(new WebSocketServer({ noServer: true, maxPayload: 1024 * 1024 }));
}
Custom servers
With a custom server, create the HTTP and WebSocket servers yourself and hand them to next-ws before Next.js starts:
import { AsyncLocalStorage } from 'node:async_hooks';
import { createServer } from 'node:http';
import next from 'next';
import { setHttpServer, setWebSocketServer } from 'next-ws/server';
import { WebSocketServer } from 'ws';
// The `next` CLI sets this up itself, a custom server has to do it too
globalThis.AsyncLocalStorage = AsyncLocalStorage;
const httpServer = createServer();
setHttpServer(httpServer);
setWebSocketServer(new WebSocketServer({ noServer: true }));
const app = next({ dev: process.env.NODE_ENV !== 'production' });
await app.prepare();
httpServer.on('request', app.getRequestHandler()).listen(3000);
Running several instances
Clients connected to different servers cannot see each other. An adapter connects the servers through a message broker, such as Redis pub/sub, so that broadcast() reaches a room's clients on every instance. Set one with setAdapter() in instrumentation.ts, or in a custom server before Next.js starts:
// instrumentation.ts
export async function register() {
if (process.env.NEXT_RUNTIME !== 'nodejs') return;
const { setAdapter } = await import('next-ws/server');
const { RedisAdapter } = await import('./lib/redis-adapter');
setAdapter(new RedisAdapter(process.env.REDIS_URL));
}
The adapter only carries what you broadcast(). next-ws subscribes an instance to a room when its first local client joins, and unsubscribes when the last one leaves.
An adapter implements four methods, and only ever handles strings:
import type { Adapter } from 'next-ws/server';
class RedisAdapter implements Adapter {
broadcast(room: string, message: string) {} // Publish to every instance
subscribe(room: string, handler: (message: string) => void) {} // Start receiving a room
unsubscribe(room: string) {} // Stop, once the last local client has left
close() {} // Clean up connections
}
See the multi-instance example for a complete Redis adapter.
🌀 Examples
Each example is a complete app you can copy.
| Example | Shows |
|---|---|
| Chat | Rooms from route params, sessions from cookies, presence, typing, reconnects. |
| Live dashboard | Server push on an interval, cleaning up, sending from a server action. |
| Custom server | Your own HTTP server, extra routes and graceful shutdown. |
| Multi-instance | Sharing rooms between servers with a Redis adapter. |
| WebSocket proxy | Forwarding connections to another WebSocket server from a catch-all route. |
🔒 HTTPS and wss://
Next WS doesn't handle TLS itself. It listens for upgrade requests on the same HTTP server that Next.js uses, so wss:// works whenever that server is reachable over HTTPS.
Reverse proxy (recommended): terminate TLS in nginx, Caddy or your load balancer and forward to next start over plain HTTP. Make sure the proxy forwards the Upgrade and Connection headers.
Custom server: create an HTTPS server with your certificate and hand it to Next WS. See the custom server example for the full setup.
import { readFileSync } from 'node:fs';
import { createServer } from 'node:https';
import { setHttpServer } from 'next-ws/server';
const httpsServer = createServer({
key: readFileSync('key.pem'),
cert: readFileSync('cert.pem'),
});
setHttpServer(httpsServer);
🧪 Development
pnpm install
pnpm test:unit # Unit tests and patch snapshots against real Next.js releases
pnpm test:integration # Real Next.js apps in dev and production, over real WebSockets
pnpm check # Types, lint, format and unused code
The integration tests use Redis when REDIS_URL is set, and a small stand-in broker otherwise.
Explorar proyectos relacionados
Cargando…
