next-ws 2.0.14 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -14
- package/dist/cli.cjs +946 -19
- package/dist/client/index.cjs +4 -5
- package/dist/server/index.cjs +184 -126
- package/dist/server/index.d.ts +43 -21
- package/package.json +25 -20
- package/dist/chunk-3RG5ZIWI.js +0 -10
- package/dist/client/index.d.cts +0 -27
- package/dist/client/index.js +0 -38
- package/dist/server/index.d.cts +0 -32
- package/dist/server/index.js +0 -186
package/README.md
CHANGED
|
@@ -50,14 +50,14 @@ To set up a WebSocket server with `next-ws`, you need to patch your local Next.j
|
|
|
50
50
|
|
|
51
51
|
## 🚀 Usage
|
|
52
52
|
|
|
53
|
-
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 `
|
|
53
|
+
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.
|
|
54
54
|
|
|
55
55
|
```js
|
|
56
|
-
export function
|
|
56
|
+
export function UPGRADE(
|
|
57
57
|
client: import('ws').WebSocket,
|
|
58
|
-
request: import('http').IncomingMessage,
|
|
59
58
|
server: import('ws').WebSocketServer,
|
|
60
|
-
|
|
59
|
+
request: import('next/server').NextRequest,
|
|
60
|
+
context: import('next-ws/server').RouteContext<'/api/ws'>,
|
|
61
61
|
) {
|
|
62
62
|
// ...
|
|
63
63
|
}
|
|
@@ -70,25 +70,24 @@ export function SOCKET(
|
|
|
70
70
|
|
|
71
71
|
### Echo Server
|
|
72
72
|
|
|
73
|
-
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 `
|
|
73
|
+
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:
|
|
74
74
|
|
|
75
75
|
```ts
|
|
76
76
|
// app/api/ws/route.ts (can be any route file in the app directory)
|
|
77
77
|
|
|
78
|
-
export function
|
|
79
|
-
client: import(
|
|
80
|
-
|
|
81
|
-
server: import("ws").WebSocketServer
|
|
78
|
+
export function UPGRADE(
|
|
79
|
+
client: import('ws').WebSocket,
|
|
80
|
+
server: import('ws').WebSocketServer
|
|
82
81
|
) {
|
|
83
|
-
console.log(
|
|
82
|
+
console.log('A client connected');
|
|
84
83
|
|
|
85
|
-
client.on(
|
|
86
|
-
console.log(
|
|
84
|
+
client.on('message', (message) => {
|
|
85
|
+
console.log('Received message:', message);
|
|
87
86
|
client.send(message);
|
|
88
87
|
});
|
|
89
88
|
|
|
90
|
-
client.
|
|
91
|
-
console.log(
|
|
89
|
+
client.once('close', () => {
|
|
90
|
+
console.log('A client disconnected');
|
|
92
91
|
});
|
|
93
92
|
}
|
|
94
93
|
```
|