next-ws 0.2.5 → 0.2.7
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 +18 -13
- package/client/context.js +3 -1
- package/package.json +1 -1
- package/server/setup.js +1 -1
- package/server/utilities/next.d.ts +1 -1
- package/server/utilities/patch.js +2 -2
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<div align="center">
|
|
2
2
|
<h1><strong>Next WS</strong></h1>
|
|
3
3
|
<i>Add support for WebSockets in Next.js 13 app directory</i><br>
|
|
4
|
-
<code>npm install next-ws</code>
|
|
4
|
+
<code>npm install next-ws ws</code>
|
|
5
5
|
</div>
|
|
6
6
|
|
|
7
7
|
<div align="center">
|
|
@@ -19,7 +19,9 @@
|
|
|
19
19
|
|
|
20
20
|
Next WS (`next-ws`) is an advanced Next.js **13** plugin designed to seamlessly integrate WebSocket server functionality into API routes within the **app directory**. With Next WS, you no longer require a separate server for WebSocket functionality.
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
> The last supported version of Next.js is 13.4.12, read more [here](https://github.com/apteryxxyz/next-ws/issues/6).
|
|
23
|
+
|
|
24
|
+
It's **important** to note that this module can only be used when working with a server. Unfortunately, in serverless environments like Vercel, WebSocket servers cannot be used. Additionally, this module was built for the app directory and is incompatible with the older pages directory.
|
|
23
25
|
|
|
24
26
|
Next WS is still pre its 1.0 release, and as such, things may change. If you find any bugs or have any suggestions, please open an issue on the GitHub repository.
|
|
25
27
|
|
|
@@ -53,7 +55,8 @@ npx next-ws-cli@latest patch
|
|
|
53
55
|
Once the patch is complete, you will need to install the Next WS package into your project.
|
|
54
56
|
|
|
55
57
|
```sh
|
|
56
|
-
npm install next-ws
|
|
58
|
+
npm install next-ws ws
|
|
59
|
+
# ws is a peer dependency, you must install it as well
|
|
57
60
|
```
|
|
58
61
|
|
|
59
62
|
### 🚓 Verify Patch (Optional)
|
|
@@ -161,20 +164,21 @@ import { useCallback, useEffect, useState } from 'react';
|
|
|
161
164
|
|
|
162
165
|
export default function Page() {
|
|
163
166
|
const ws = useWebSocket();
|
|
164
|
-
//
|
|
167
|
+
// ^? WebSocket on the client, null on the server
|
|
165
168
|
|
|
166
169
|
const [value, setValue] = useState('');
|
|
167
|
-
const [message, setMessage] = useState<string | null>(
|
|
168
|
-
|
|
169
|
-
const onMessage = useCallback((event: MessageEvent) => {
|
|
170
|
-
const text = await event.data.text();
|
|
171
|
-
setMessage(text);
|
|
172
|
-
}, []);
|
|
170
|
+
const [message, setMessage] = useState<string | null>(null);
|
|
173
171
|
|
|
172
|
+
const onMessage = useCallback(
|
|
173
|
+
(event: MessageEvent<Blob>) =>
|
|
174
|
+
void event.data.text().then(setMessage),
|
|
175
|
+
[],
|
|
176
|
+
);
|
|
177
|
+
|
|
174
178
|
useEffect(() => {
|
|
175
179
|
ws?.addEventListener('message', onMessage);
|
|
176
180
|
return () => ws?.removeEventListener('message', onMessage);
|
|
177
|
-
}, [ws]);
|
|
181
|
+
}, [onMessage, ws]);
|
|
178
182
|
|
|
179
183
|
return <>
|
|
180
184
|
<input
|
|
@@ -183,15 +187,16 @@ export default function Page() {
|
|
|
183
187
|
onChange={event => setValue(event.target.value)}
|
|
184
188
|
/>
|
|
185
189
|
|
|
186
|
-
<button onClick={() => ws
|
|
190
|
+
<button onClick={() => ws?.send(value)}>
|
|
187
191
|
Send message to server
|
|
188
192
|
</button>
|
|
189
193
|
|
|
190
194
|
<p>
|
|
191
195
|
{message === null
|
|
192
|
-
? 'Waiting
|
|
196
|
+
? 'Waiting to receive message...'
|
|
193
197
|
: `Got message: ${message}`}
|
|
194
198
|
</p>
|
|
195
199
|
</>;
|
|
196
200
|
}
|
|
201
|
+
|
|
197
202
|
```
|
package/client/context.js
CHANGED
|
@@ -22,7 +22,9 @@ function WebSocketProvider({ children, url, protocols, binaryType, }) {
|
|
|
22
22
|
return client;
|
|
23
23
|
}, [isBrowser, url, protocols]);
|
|
24
24
|
(0, react_1.useEffect)(() => {
|
|
25
|
-
|
|
25
|
+
if (instance?.readyState !== WebSocket.OPEN)
|
|
26
|
+
return;
|
|
27
|
+
return () => instance.close();
|
|
26
28
|
}, []);
|
|
27
29
|
return (0, jsx_runtime_1.jsx)(exports.WebSocketContext.Provider, { value: instance, children: children });
|
|
28
30
|
}
|
package/package.json
CHANGED
package/server/setup.js
CHANGED
|
@@ -25,7 +25,7 @@ function setupWebSocketServer(nextServer) {
|
|
|
25
25
|
Log.error(`[next-ws] could not find module for page ${pathname}`);
|
|
26
26
|
return socket.destroy();
|
|
27
27
|
}
|
|
28
|
-
const socketHandler = pageModule?.routeModule?.
|
|
28
|
+
const socketHandler = pageModule?.routeModule?.userland?.SOCKET;
|
|
29
29
|
if (!socketHandler || typeof socketHandler !== 'function') {
|
|
30
30
|
Log.error(`[next-ws] ${pathname} does not export a SOCKET handler`);
|
|
31
31
|
return socket.destroy();
|
|
@@ -24,7 +24,7 @@ export declare function resolvePathname(nextServer: NextNodeServer, pathname: st
|
|
|
24
24
|
export declare function getPageModule(nextServer: NextNodeServer, filename: string): Promise<PageModule>;
|
|
25
25
|
export interface PageModule {
|
|
26
26
|
routeModule?: {
|
|
27
|
-
|
|
27
|
+
userland?: {
|
|
28
28
|
SOCKET?: SocketHandler;
|
|
29
29
|
};
|
|
30
30
|
};
|
|
@@ -27,11 +27,11 @@ exports.getPatch = getPatch;
|
|
|
27
27
|
function verifyPatch() {
|
|
28
28
|
const patch = getPatch();
|
|
29
29
|
if (!patch)
|
|
30
|
-
throw new Error('Next.js has not been patched to support Next WS, please run `npx next-ws-cli patch`');
|
|
30
|
+
throw new Error('Next.js has not been patched to support Next WS, please run `npx next-ws-cli@latest patch`');
|
|
31
31
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
32
32
|
const packageJson = require('next/package.json');
|
|
33
33
|
const version = packageJson.version.split('-')[0];
|
|
34
34
|
if (patch.version !== version)
|
|
35
|
-
throw new Error(`Next.js version mismatch, expected ${patch.version} but found ${version}, try running \`npx next-ws-cli patch\``);
|
|
35
|
+
throw new Error(`Next.js version mismatch, expected ${patch.version} but found ${version}, try running \`npx next-ws-cli@latest patch\``);
|
|
36
36
|
}
|
|
37
37
|
exports.verifyPatch = verifyPatch;
|