https://github.com/k0d13/next-ws
WebSockets inside Next.js app routes
https://github.com/k0d13/next-ws
module node
Last synced: about 2 months ago
JSON representation
WebSockets inside Next.js app routes
- Host: GitHub
- URL: https://github.com/k0d13/next-ws
- Owner: k0d13
- Created: 2023-06-04T19:31:11.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2026-04-01T09:04:31.000Z (about 2 months ago)
- Last Synced: 2026-04-01T09:23:34.027Z (about 2 months ago)
- Topics: module, node
- Language: TypeScript
- Homepage: https://npmjs.com/next-ws
- Size: 1.88 MB
- Stars: 316
- Watchers: 3
- Forks: 24
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
Next WS
Add support for WebSockets in Next.js app directory
npm 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](#-installation)
- [🚀 Usage](#-usage)
- [🌀 Examples](#-examples)
## 📦 Installation
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:
1. **Install Dependencies**: Use your preferred package manager to install `next-ws` and its peer dependency `ws`:
```bash
npm install next-ws ws
pnpm add next-ws ws
yarn add next-ws ws
```
2. **Add Prepare Script**: Add the following `prepare` script to your `package.json`. The `prepare` script is a lifecycle script that runs automatically when you run `npm install`, ensuring that your Next.js installation is patched with `next-ws` every time you install it:
```json
{
"scripts": {
"prepare": "next-ws patch"
}
}
```
## 🚀 Usage
Using WebSocket connections in your Next.js app directory is simple with `next-ws`. You can handle WebSocket connections directly in your API routes via exported `UPGRADE` functions.
```js
export function UPGRADE(
client: import('ws').WebSocket,
server: import('ws').WebSocketServer,
request: import('next/server').NextRequest,
context: import('next-ws/server').RouteContext<'/api/ws'>,
) {
// ...
}
```
## 🌀 Examples
> [!TIP]
> For more detailed examples, refer the [`examples` directory](https://github.com/k0d13/next-ws/tree/main/examples).
### Echo Server
This example demonstrates a simple WebSocket echo server that sends back any message it receives. Create a new API route file anywhere in your app directory and export a `UPGRADE` function to handle WebSocket connections:
```ts
// app/api/ws/route.ts (can be any route file in the app directory)
export function UPGRADE(
client: import('ws').WebSocket,
server: import('ws').WebSocketServer
) {
console.log('A client connected');
client.on('message', (message) => {
console.log('Received message:', message);
client.send(message);
});
client.once('close', () => {
console.log('A client disconnected');
});
}
```
You can now connect to your WebSocket server, send it a message and receive the same message back.
### Chat Room
See the [chat room example](https://github.com/k0d13/next-ws/tree/main/examples/chat-room).
