@usermetrics/queuebit 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/LICENSE +21 -0
- package/README.md +276 -0
- package/docs/API.md +577 -0
- package/docs/EXAMPLES.md +330 -0
- package/docs/QUICKSTART.md +122 -0
- package/examples/browser-example.html +573 -0
- package/package.json +52 -0
- package/src/client-browser.js +134 -0
- package/src/client-node.js +112 -0
- package/src/client.js +73 -0
- package/src/index.js +7 -0
- package/src/server-runner.js +30 -0
- package/src/server.js +292 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 QueueBit Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
# QueueBit
|
|
2
|
+
|
|
3
|
+
A high performance socket-based message queue server with guaranteed delivery, compatible with NATS queue patterns.
|
|
4
|
+
|
|
5
|
+
It can run in-process in an existing nodejs app, separately as a nodejs server, and has clients
|
|
6
|
+
for the backend and frontend.
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- WebSocket-based message queue
|
|
11
|
+
- Subject-based message routing
|
|
12
|
+
- Queue groups for load-balanced message delivery
|
|
13
|
+
- Message expiry support
|
|
14
|
+
- Remove after read (ephemeral messages)
|
|
15
|
+
- Guaranteed delivery to all subscribers
|
|
16
|
+
- NATS-compatible API patterns
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install queuebit
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Documentation
|
|
25
|
+
|
|
26
|
+
- **[Quick Start Guide](./docs/QUICKSTART.md)** - Get started in 5 minutes
|
|
27
|
+
- **[API Reference](./docs/API.md)** - Complete API documentation
|
|
28
|
+
- **[Examples](./docs/EXAMPLES.md)** - Practical examples for common use cases
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
### Running the Server
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm run server
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Or with custom options:
|
|
39
|
+
```bash
|
|
40
|
+
node src/server-runner.js --port=4000 --max-queue=5000
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
On Windows:
|
|
44
|
+
```cmd
|
|
45
|
+
start-server.cmd
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Node.js Client
|
|
49
|
+
|
|
50
|
+
```javascript
|
|
51
|
+
const { QueueBitClient } = require('queuebit');
|
|
52
|
+
|
|
53
|
+
const client = new QueueBitClient('http://localhost:3000');
|
|
54
|
+
|
|
55
|
+
// Subscribe to messages
|
|
56
|
+
await client.subscribe((message) => {
|
|
57
|
+
console.log('Received:', message.data);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Publish a message
|
|
61
|
+
await client.publish({ hello: 'world' });
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Browser Client
|
|
65
|
+
|
|
66
|
+
Include Socket.IO and QueueBit client in your HTML:
|
|
67
|
+
|
|
68
|
+
```html
|
|
69
|
+
<script src="https://cdn.socket.io/4.7.2/socket.io.min.js"></script>
|
|
70
|
+
<script src="node_modules/queuebit/src/client-browser.js"></script>
|
|
71
|
+
|
|
72
|
+
<script>
|
|
73
|
+
const client = new QueueBitClient('http://localhost:3000');
|
|
74
|
+
|
|
75
|
+
// Subscribe to messages
|
|
76
|
+
client.subscribe((message) => {
|
|
77
|
+
console.log('Received:', message.data);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// Publish a message
|
|
81
|
+
client.publish({ hello: 'world from browser!' });
|
|
82
|
+
</script>
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
See `examples/browser-example.html` for a complete browser example.
|
|
86
|
+
|
|
87
|
+
### Server
|
|
88
|
+
|
|
89
|
+
```javascript
|
|
90
|
+
const { QueueBitServer } = require('queuebit');
|
|
91
|
+
|
|
92
|
+
const server = new QueueBitServer({
|
|
93
|
+
port: 3000,
|
|
94
|
+
maxQueueSize: 10000
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Client
|
|
99
|
+
|
|
100
|
+
```javascript
|
|
101
|
+
const { QueueBitClient } = require('queuebit');
|
|
102
|
+
|
|
103
|
+
const client = new QueueBitClient('http://localhost:3000');
|
|
104
|
+
|
|
105
|
+
// Subscribe to messages
|
|
106
|
+
await client.subscribe((message) => {
|
|
107
|
+
console.log('Received:', message.data);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Publish a message
|
|
111
|
+
await client.publish({ hello: 'world' });
|
|
112
|
+
|
|
113
|
+
// Subject-based routing
|
|
114
|
+
await client.subscribe((message) => {
|
|
115
|
+
console.log('Order:', message.data);
|
|
116
|
+
}, { subject: 'orders' });
|
|
117
|
+
|
|
118
|
+
await client.publish({ orderId: 123 }, { subject: 'orders' });
|
|
119
|
+
|
|
120
|
+
// Queue groups (load balanced)
|
|
121
|
+
await client.subscribe((message) => {
|
|
122
|
+
console.log('Worker received:', message.data);
|
|
123
|
+
}, { subject: 'tasks', queue: 'workers' });
|
|
124
|
+
|
|
125
|
+
// Message with expiry
|
|
126
|
+
const expiryDate = new Date(Date.now() + 60000); // 1 minute
|
|
127
|
+
await client.publish({ data: 'expires soon' }, {
|
|
128
|
+
expiry: expiryDate
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// One-time read message
|
|
132
|
+
await client.publish({ data: 'read once' }, {
|
|
133
|
+
removeAfterRead: true
|
|
134
|
+
});
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Performance
|
|
138
|
+
|
|
139
|
+
QueueBit is optimized for high throughput:
|
|
140
|
+
|
|
141
|
+
- **WebSocket-only transport** for reduced overhead
|
|
142
|
+
- **Batch message processing** on the server
|
|
143
|
+
- **Async delivery** to prevent blocking
|
|
144
|
+
- **No compression** for maximum speed
|
|
145
|
+
- **Typical throughput**: 20,000-50,000+ messages/second (depends on hardware and message size)
|
|
146
|
+
|
|
147
|
+
### Performance Tips
|
|
148
|
+
|
|
149
|
+
1. Use WebSocket transport only (default)
|
|
150
|
+
2. Send messages in batches when possible
|
|
151
|
+
3. Avoid very large message payloads
|
|
152
|
+
4. Use queue groups for load balancing across multiple consumers
|
|
153
|
+
5. Monitor queue sizes to prevent memory issues
|
|
154
|
+
|
|
155
|
+
## API
|
|
156
|
+
|
|
157
|
+
### QueueBitServer
|
|
158
|
+
|
|
159
|
+
#### Constructor Options
|
|
160
|
+
- `port` (number): Server port (default: 3000)
|
|
161
|
+
- `maxQueueSize` (number): Maximum messages per subject (default: 10000)
|
|
162
|
+
|
|
163
|
+
### QueueBitClient
|
|
164
|
+
|
|
165
|
+
#### Methods
|
|
166
|
+
|
|
167
|
+
##### `publish(message, options)`
|
|
168
|
+
Publish a message to the queue.
|
|
169
|
+
|
|
170
|
+
Options:
|
|
171
|
+
- `subject` (string): Message subject/topic
|
|
172
|
+
- `expiry` (Date): Message expiration date
|
|
173
|
+
- `removeAfterRead` (boolean): Remove message after first delivery
|
|
174
|
+
|
|
175
|
+
##### `subscribe(callback, options)`
|
|
176
|
+
Subscribe to messages.
|
|
177
|
+
|
|
178
|
+
Options:
|
|
179
|
+
- `subject` (string): Subscribe to specific subject
|
|
180
|
+
- `queue` (string): Join a queue group for load-balanced delivery
|
|
181
|
+
|
|
182
|
+
##### `unsubscribe(options)`
|
|
183
|
+
Unsubscribe from messages.
|
|
184
|
+
|
|
185
|
+
##### `disconnect()`
|
|
186
|
+
Disconnect from the server.
|
|
187
|
+
|
|
188
|
+
## Publishing to NPM
|
|
189
|
+
|
|
190
|
+
### First Time Setup
|
|
191
|
+
|
|
192
|
+
1. Create an NPM account at https://www.npmjs.com/signup
|
|
193
|
+
2. Run authentication setup:
|
|
194
|
+
```cmd
|
|
195
|
+
setup-npm-auth.cmd
|
|
196
|
+
```
|
|
197
|
+
3. Update package.json with your username:
|
|
198
|
+
- Change `@yourusername/queuebit` to `@YOUR_NPM_USERNAME/queuebit`
|
|
199
|
+
- Or use an unscoped name like `queuebit-yourname` if available
|
|
200
|
+
- Update author field with your information
|
|
201
|
+
|
|
202
|
+
### Publishing
|
|
203
|
+
|
|
204
|
+
1. Update version number:
|
|
205
|
+
```cmd
|
|
206
|
+
update-version.cmd
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
2. **Without 2FA:**
|
|
210
|
+
```cmd
|
|
211
|
+
publish.cmd
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
3. **With 2FA enabled:**
|
|
215
|
+
```cmd
|
|
216
|
+
publish-with-otp.cmd
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### Troubleshooting
|
|
220
|
+
|
|
221
|
+
- **403 Forbidden / 2FA Required**: Use `publish-with-otp.cmd`
|
|
222
|
+
- **Package name taken**: Change name in package.json to something unique
|
|
223
|
+
- **Not logged in**: Run `npm login` or `setup-npm-auth.cmd`
|
|
224
|
+
- **Version already exists**: Increment version with `update-version.cmd`
|
|
225
|
+
|
|
226
|
+
## Development
|
|
227
|
+
|
|
228
|
+
### Install Dependencies
|
|
229
|
+
```bash
|
|
230
|
+
npm install
|
|
231
|
+
```
|
|
232
|
+
Or on Windows:
|
|
233
|
+
```cmd
|
|
234
|
+
install-deps.cmd
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### Run Tests
|
|
238
|
+
```bash
|
|
239
|
+
npm test
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
### Publishing
|
|
243
|
+
|
|
244
|
+
#### Update Version
|
|
245
|
+
```cmd
|
|
246
|
+
update-version.cmd
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
#### Dry Run (test without publishing)
|
|
250
|
+
```cmd
|
|
251
|
+
publish-dry-run.cmd
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
#### Publish to NPM
|
|
255
|
+
```cmd
|
|
256
|
+
publish.cmd
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
Or manually:
|
|
260
|
+
```bash
|
|
261
|
+
npm login
|
|
262
|
+
npm test
|
|
263
|
+
npm publish
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## Testing
|
|
267
|
+
|
|
268
|
+
```bash
|
|
269
|
+
npm test
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
## License
|
|
273
|
+
|
|
274
|
+
MIT License - See [LICENSE](LICENSE) file for details.
|
|
275
|
+
|
|
276
|
+
This software is free to use with attribution. You must include the copyright notice and license text in all copies or substantial portions of the software.
|