next-ws 2.0.14 → 2.1.1

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 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 `SOCKET` functions. Here's an example of a simple WebSocket echo server:
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 SOCKET(
56
+ export function UPGRADE(
57
57
  client: import('ws').WebSocket,
58
- request: import('http').IncomingMessage,
59
58
  server: import('ws').WebSocketServer,
60
- context: { params: Record<string, string | string[]> },
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 `SOCKET` function to handle WebSocket connections:
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 SOCKET(
79
- client: import("ws").WebSocket,
80
- request: import("http").IncomingMessage,
81
- server: import("ws").WebSocketServer
78
+ export function UPGRADE(
79
+ client: import('ws').WebSocket,
80
+ server: import('ws').WebSocketServer
82
81
  ) {
83
- console.log("A client connected");
82
+ console.log('A client connected');
84
83
 
85
- client.on("message", (message) => {
86
- console.log("Received message:", message);
84
+ client.on('message', (message) => {
85
+ console.log('Received message:', message);
87
86
  client.send(message);
88
87
  });
89
88
 
90
- client.on("close", () => {
91
- console.log("A client disconnected");
89
+ client.once('close', () => {
90
+ console.log('A client disconnected');
92
91
  });
93
92
  }
94
93
  ```