next-ws 1.2.0 → 2.0.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 ADDED
@@ -0,0 +1,100 @@
1
+ <div align='center'>
2
+ <h1><strong>Next WS</strong></h1>
3
+ <i>Add support for WebSockets in Next.js app directory</i><br>
4
+ <code>npm install next-ws ws</code>
5
+ </div>
6
+
7
+ <div align='center'>
8
+ <img alt='package version' src='https://img.shields.io/npm/v/next-ws?label=version'>
9
+ <img alt='total downloads' src='https://img.shields.io/npm/dt/next-ws'>
10
+ <br>
11
+ <a href='https://github.com/apteryxxyz/next-ws'><img alt='next-ws repo stars' src='https://img.shields.io/github/stars/apteryxxyz/next-ws?style=social'></a>
12
+ <a href='https://github.com/apteryxxyz'><img alt='apteryxxyz followers' src='https://img.shields.io/github/followers/apteryxxyz?style=social'></a>
13
+ <a href='https://discord.gg/B2rEQ9g2vf'><img src='https://discordapp.com/api/guilds/829836158007115806/widget.png?style=shield' alt='discord shield'/></a>
14
+ </div>
15
+
16
+ ## 🤔 About
17
+
18
+ `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.
19
+
20
+ > [!IMPORTANT]
21
+ > 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.
22
+
23
+ ## 🏓 Table of Contents
24
+
25
+ - [📦 Installation](#-installation)
26
+ - [🚀 Usage](#-usage)
27
+ - [🌀 Examples](#-examples)
28
+
29
+ ## 📦 Installation
30
+
31
+ 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:
32
+
33
+ 1. **Install Dependencies**: Use your preferred package manager to install `next-ws` and its peer dependency `ws`:
34
+
35
+ ```bash
36
+ npm install next-ws ws
37
+ pnpm add next-ws ws
38
+ yarn add next-ws ws
39
+ ```
40
+
41
+ 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:
42
+
43
+ ```json
44
+ {
45
+ "scripts": {
46
+ "prepare": "next-ws patch"
47
+ }
48
+ }
49
+ ```
50
+
51
+ ## 🚀 Usage
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:
54
+
55
+ ```js
56
+ export function SOCKET(
57
+ client: import('ws').WebSocket,
58
+ request: import('http').IncomingMessage,
59
+ server: import('ws').WebSocketServer,
60
+ context: { params: Record<string, string | string[]> },
61
+ ) {
62
+ // ...
63
+ }
64
+ ```
65
+
66
+ ## 🌀 Examples
67
+
68
+ > [!TIP]
69
+ > For more detailed examples, refer the [`examples` directory](https://github.com/apteryxxyz/next-ws/tree/main/examples).
70
+
71
+ ### Echo Server
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:
74
+
75
+ ```ts
76
+ // app/api/ws/route.ts (can be any route file in the app directory)
77
+
78
+ export function SOCKET(
79
+ client: import("ws").WebSocket,
80
+ request: import("http").IncomingMessage,
81
+ server: import("ws").WebSocketServer
82
+ ) {
83
+ console.log("A client connected");
84
+
85
+ client.on("message", (message) => {
86
+ console.log("Received message:", message);
87
+ client.send(message);
88
+ });
89
+
90
+ client.on("close", () => {
91
+ console.log("A client disconnected");
92
+ });
93
+ }
94
+ ```
95
+
96
+ You can now connect to your WebSocket server, send it a message and receive the same message back.
97
+
98
+ ### Chat Room
99
+
100
+ See the [chat room example](https://github.com/apteryxxyz/next-ws/tree/main/examples/chat-room).
@@ -0,0 +1,10 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
8
+ export {
9
+ __require
10
+ };