uwebsockets 3.0.1 → 3.0.2
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 +147 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
|
|
15
15
|
- Browse the [documentation](https://unetworking.github.io/uwebsockets/generated/) and see the [main repo](https://github.com/NPM-Package-Jaykumar/uWebSockets). There are tons of [examples](examples) but here's the gist of it all:
|
|
16
16
|
|
|
17
|
+
Example 1:
|
|
18
|
+
|
|
17
19
|
```javascript
|
|
18
20
|
/* Non-SSL is simply App() */
|
|
19
21
|
require("uwebsockets")
|
|
@@ -50,3 +52,148 @@ require("uwebsockets")
|
|
|
50
52
|
}
|
|
51
53
|
});
|
|
52
54
|
```
|
|
55
|
+
|
|
56
|
+
Example 2:
|
|
57
|
+
|
|
58
|
+
```javascript
|
|
59
|
+
const uWS = require("uwebsockets");
|
|
60
|
+
|
|
61
|
+
const WS_INSTANCES = {};
|
|
62
|
+
|
|
63
|
+
// Below is a sample UWS server that listens for WebSocket connections and handles messages
|
|
64
|
+
// from clients. It also includes a simple authentication mechanism based on a query parameter.
|
|
65
|
+
// This is a basic example and should be adapted to your specific use case and security requirements.
|
|
66
|
+
// This example uses uwebsockets for WebSocket handling.
|
|
67
|
+
// You can install uwebsockets using npm:
|
|
68
|
+
// npm install uwebsockets
|
|
69
|
+
// Note: Make sure to replace the placeholder code with your actual logic.
|
|
70
|
+
|
|
71
|
+
const wsApp = uWS.App().ws("/*", {
|
|
72
|
+
// Options for WebSocket server
|
|
73
|
+
compression: 0, // No compression
|
|
74
|
+
maxPayloadLength: 16 * 1024 * 1024, // 16 MB max payload size
|
|
75
|
+
idleTimeout: 8, // 8 seconds idle timeout
|
|
76
|
+
|
|
77
|
+
upgrade: (res, req, context) => {
|
|
78
|
+
// Check if the request has a valid "channel_id" query parameter
|
|
79
|
+
const channel_id = req.getQuery("channel_id");
|
|
80
|
+
|
|
81
|
+
// Check if the channel_id is valid (you can implement your own validation logic). below is an example
|
|
82
|
+
if (!channel_id) {
|
|
83
|
+
// Invalid channel_id, send a 400 Bad Request response
|
|
84
|
+
res
|
|
85
|
+
.writeStatus("400 Bad Request! missing channel_id in query parameter")
|
|
86
|
+
.end();
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Store the channel_id in the WebSocket connection
|
|
91
|
+
res.upgrade(
|
|
92
|
+
{ channel_id },
|
|
93
|
+
req.getHeader("sec-websocket-key"),
|
|
94
|
+
req.getHeader("sec-websocket-protocol"),
|
|
95
|
+
req.getHeader("sec-websocket-extensions"),
|
|
96
|
+
context
|
|
97
|
+
);
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
open: (ws) => {
|
|
101
|
+
// Check if the channel_id is already connected
|
|
102
|
+
const channel_id = ws.channel_id;
|
|
103
|
+
|
|
104
|
+
// Store the WebSocket connection in the WS_INSTANCES object
|
|
105
|
+
WS_INSTANCES[channel_id] = WS_INSTANCES[channel_id] || [];
|
|
106
|
+
WS_INSTANCES[channel_id].push(ws);
|
|
107
|
+
|
|
108
|
+
// Send a welcome message to the client
|
|
109
|
+
const welcomePayload = {
|
|
110
|
+
event: "welcome",
|
|
111
|
+
message: `Welcome ${channel_id} to the WebSocket server!`,
|
|
112
|
+
timestamp: Date.now(),
|
|
113
|
+
};
|
|
114
|
+
ws.send(JSON.stringify(welcomePayload));
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
message: (ws, message) => {
|
|
118
|
+
try {
|
|
119
|
+
// Parse the message and handle it
|
|
120
|
+
const channel_id = ws.channel_id;
|
|
121
|
+
|
|
122
|
+
// Buffer.from(message).toString() is used to convert the message to a string
|
|
123
|
+
// This is necessary because uwebsockets may send binary messages
|
|
124
|
+
// If you are sending text messages, you can directly use message.toString()
|
|
125
|
+
// If you are sending binary messages, you may need to convert them to a string first, Buffer.from(message).toString()
|
|
126
|
+
const msg = Buffer.from(message).toString();
|
|
127
|
+
const parsedMsg = JSON.parse(msg);
|
|
128
|
+
|
|
129
|
+
// Payload to be sent to the channel_id
|
|
130
|
+
// You can customize the payload structure based on your requirements
|
|
131
|
+
// For example, you can include the channel_id, event type, and data
|
|
132
|
+
const payload = {
|
|
133
|
+
event: "broadcast",
|
|
134
|
+
from: channel_id,
|
|
135
|
+
data: parsedMsg,
|
|
136
|
+
timestamp: Date.now(),
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// Example: Echo the message back to the same user (or broadcast as needed)
|
|
140
|
+
ws.send(JSON.stringify(payload));
|
|
141
|
+
} catch (error) {
|
|
142
|
+
// Handle errors that occur during message processing
|
|
143
|
+
// For example, if the message is not a valid JSON string or if there is an error in your logic
|
|
144
|
+
// You can send an error message back to the client
|
|
145
|
+
// and log the error for debugging purposes
|
|
146
|
+
// Buffer.from(message).toString() is used to convert the message to a string
|
|
147
|
+
// This is necessary because uwebsockets may send binary messages
|
|
148
|
+
// If you are sending text messages, you can directly use message.toString()
|
|
149
|
+
const msg = Buffer.from(message).toString();
|
|
150
|
+
|
|
151
|
+
// error payload to be sent to the channel_id
|
|
152
|
+
// You can customize the error payload structure based on your requirements
|
|
153
|
+
const errorPayload = {
|
|
154
|
+
event: "error",
|
|
155
|
+
from: ws.channel_id,
|
|
156
|
+
message: error.message,
|
|
157
|
+
data: JSON.stringify(msg),
|
|
158
|
+
timestamp: Date.now(),
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
// Send the error message back to the client
|
|
162
|
+
ws.send(JSON.stringify(errorPayload));
|
|
163
|
+
console.error("Error processing message:", {
|
|
164
|
+
error,
|
|
165
|
+
message: JSON.stringify(msg),
|
|
166
|
+
ws: ws.channel_id,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
close: (ws) => {
|
|
171
|
+
// Handle WebSocket disconnection
|
|
172
|
+
const channel_id = ws.channel_id;
|
|
173
|
+
|
|
174
|
+
// Remove the WebSocket connection from the WS_INSTANCES object
|
|
175
|
+
// This is done by filtering out the closed connection from the array of connections for the channel_id
|
|
176
|
+
// If there are no more connections for the channel_id, delete the channel_id entry from WS_INSTANCES
|
|
177
|
+
// This ensures that the server does not keep track of closed connections
|
|
178
|
+
// and helps to free up resources
|
|
179
|
+
WS_INSTANCES[channel_id] =
|
|
180
|
+
WS_INSTANCES[channel_id]?.filter((conn) => conn !== ws) || [];
|
|
181
|
+
if (WS_INSTANCES[channel_id].length === 0) delete WS_INSTANCES[channel_id];
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
// Start the WebSocket server
|
|
186
|
+
// Listen using the properly defined wsApp instance
|
|
187
|
+
wsApp.listen(9001, (token) => {
|
|
188
|
+
if (token) {
|
|
189
|
+
console.log(`WebSocket listening on ws://localhost:${9001}`);
|
|
190
|
+
} else {
|
|
191
|
+
console.error(`Failed to listen on port ${9001}`);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
// Note: Make sure to handle errors and edge cases in your production code.
|
|
195
|
+
// This example is a basic starting point and should be adapted to your specific use case.
|
|
196
|
+
// You can also implement additional features such as authentication, authorization, and message validation
|
|
197
|
+
// based on your requirements.
|
|
198
|
+
// For more information on uwebsockets, refer to the official documentation
|
|
199
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "uwebsockets",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.2",
|
|
4
4
|
"description": "Fastest uWebSockets server for Node.js – µWebSockets outperforms Socket.IO & Fastify. Built in C++ for high-performance networking & pub/sub.",
|
|
5
5
|
"main": "uws.js",
|
|
6
6
|
"types": "./index.d.ts",
|