wxexpress 1.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 +150 -0
- package/cjsindex.cjs +46439 -0
- package/mjsindex.mjs +46437 -0
- package/package.json +47 -0
package/README.md
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
wsexpress
|
|
2
|
+
WebSocket routing for Express applications.
|
|
3
|
+
wsexpress lets you define WebSocket endpoints exactly like normal Express routes and allows you to use Express middleware with WebSockets.
|
|
4
|
+
It internally uses the powerful ws WebSocket library.
|
|
5
|
+
|
|
6
|
+
✅ Features
|
|
7
|
+
•
|
|
8
|
+
Define WebSocket routes just like HTTP routes
|
|
9
|
+
•
|
|
10
|
+
Works with Express middleware
|
|
11
|
+
•
|
|
12
|
+
Supports Express Routers
|
|
13
|
+
•
|
|
14
|
+
Built on top of the fast ws library
|
|
15
|
+
•
|
|
16
|
+
Simple API and lightweight
|
|
17
|
+
|
|
18
|
+
📦 Installation
|
|
19
|
+
bash
|
|
20
|
+
Copy
|
|
21
|
+
npm install --save wsexpress
|
|
22
|
+
|
|
23
|
+
🚀 Quick Start
|
|
24
|
+
First, attach wsexpress to your Express app:
|
|
25
|
+
javascript
|
|
26
|
+
Copy
|
|
27
|
+
const express = require('express');
|
|
28
|
+
const app = express();
|
|
29
|
+
|
|
30
|
+
require('wsexpress')(app);
|
|
31
|
+
⚠️ Important:
|
|
32
|
+
Initialize wsexpress before defining routers or routes.
|
|
33
|
+
Otherwise, you may see an error like:
|
|
34
|
+
vbnet
|
|
35
|
+
Copy
|
|
36
|
+
router.ws is not a function
|
|
37
|
+
|
|
38
|
+
🧩 Basic WebSocket Route
|
|
39
|
+
Create a simple echo WebSocket server:
|
|
40
|
+
javascript
|
|
41
|
+
Copy
|
|
42
|
+
app.ws('/echo', (ws, req) => {
|
|
43
|
+
ws.on('message', msg => {
|
|
44
|
+
ws.send(msg);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
The ws object is a standard WebSocket instance from the ws library.
|
|
48
|
+
|
|
49
|
+
📂 Using with Express Router
|
|
50
|
+
You can also use WebSocket routes inside routers:
|
|
51
|
+
javascript
|
|
52
|
+
Copy
|
|
53
|
+
const express = require('express');
|
|
54
|
+
const router = express.Router();
|
|
55
|
+
|
|
56
|
+
router.ws('/echo', (ws, req) => {
|
|
57
|
+
ws.on('message', msg => {
|
|
58
|
+
ws.send(msg);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
app.use('/ws-stuff', router);
|
|
63
|
+
This creates a WebSocket endpoint at:
|
|
64
|
+
bash
|
|
65
|
+
Copy
|
|
66
|
+
/ws-stuff/echo
|
|
67
|
+
|
|
68
|
+
🧪 Full Example
|
|
69
|
+
javascript
|
|
70
|
+
Copy
|
|
71
|
+
const express = require('express');
|
|
72
|
+
const app = express();
|
|
73
|
+
|
|
74
|
+
const wsExpress = require('wsexpress')(app);
|
|
75
|
+
|
|
76
|
+
// middleware
|
|
77
|
+
app.use((req, res, next) => {
|
|
78
|
+
console.log('middleware executed');
|
|
79
|
+
req.testing = 'testing';
|
|
80
|
+
next();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// HTTP route
|
|
84
|
+
app.get('/', (req, res) => {
|
|
85
|
+
console.log('GET route:', req.testing);
|
|
86
|
+
res.end('Hello HTTP');
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// WebSocket route
|
|
90
|
+
app.ws('/', (ws, req) => {
|
|
91
|
+
console.log('WebSocket connected:', req.testing);
|
|
92
|
+
|
|
93
|
+
ws.on('message', msg => {
|
|
94
|
+
console.log('Received:', msg.toString());
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
app.listen(3000, () => {
|
|
99
|
+
console.log('Server running on port 3000');
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
📘 API
|
|
103
|
+
wsexpress(app, server?, options?)
|
|
104
|
+
Initializes WebSocket support on an Express app.
|
|
105
|
+
Parameters
|
|
106
|
+
•
|
|
107
|
+
app
|
|
108
|
+
Your Express application.
|
|
109
|
+
•
|
|
110
|
+
server (optional)
|
|
111
|
+
Custom http.Server instance.
|
|
112
|
+
If not provided, the server created by app.listen() is used.
|
|
113
|
+
•
|
|
114
|
+
options (optional)
|
|
115
|
+
Configuration object:
|
|
116
|
+
•
|
|
117
|
+
leaveRouterUntouched (boolean)
|
|
118
|
+
If true, routers are not automatically patched.
|
|
119
|
+
You must manually apply WebSocket support using applyTo().
|
|
120
|
+
•
|
|
121
|
+
wsOptions (object)
|
|
122
|
+
Options passed directly to the WebSocketServer.
|
|
123
|
+
|
|
124
|
+
wsInstance.app
|
|
125
|
+
Returns the Express app instance used by wsexpress.
|
|
126
|
+
|
|
127
|
+
wsInstance.getWss()
|
|
128
|
+
Returns the underlying WebSocket server.
|
|
129
|
+
Example:
|
|
130
|
+
javascript
|
|
131
|
+
Copy
|
|
132
|
+
wsInstance.getWss().clients
|
|
133
|
+
⚠️ This returns all connected clients across all routes, so use carefully.
|
|
134
|
+
|
|
135
|
+
wsInstance.applyTo(router)
|
|
136
|
+
Manually enables WebSocket support for a router.
|
|
137
|
+
You only need this if:
|
|
138
|
+
1.
|
|
139
|
+
leaveRouterUntouched is enabled, or
|
|
140
|
+
2.
|
|
141
|
+
You are using a custom router implementation.
|
|
142
|
+
|
|
143
|
+
🛠 Development
|
|
144
|
+
•
|
|
145
|
+
Written in ES6
|
|
146
|
+
•
|
|
147
|
+
Uses ESM internally
|
|
148
|
+
•
|
|
149
|
+
Lightweight and production ready
|
|
150
|
+
|