@usermetrics/queuebit 1.0.0 → 1.0.5

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 CHANGED
@@ -1,25 +1,29 @@
1
1
  # QueueBit
2
2
 
3
- A high performance socket-based message queue server with guaranteed delivery, compatible with NATS queue patterns.
3
+ A high performance socket-based message queue server with guaranteed delivery, compatible with NATS queue patterns.
4
+ Built-in Load Balancer with round-robin delivery. (see examples).
5
+
6
+ It can run in-process in an existing Node.js app, separately as a standalone server, or run clients
7
+ in the backend and/or frontend.
8
+ A frontend in examples (qpanel.html) can help test the server.
9
+
10
+ ## Installation
11
+ ```
12
+ npm install @usermetrics/queuebit
13
+ ```
4
14
 
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
15
 
8
16
  ## Features
9
17
 
10
18
  - WebSocket-based message queue
11
19
  - Subject-based message routing
12
- - Queue groups for load-balanced message delivery
20
+ - Load balancer with round-robin delivery (each message goes to exactly one worker)
13
21
  - Message expiry support
14
- - Remove after read (ephemeral messages)
15
- - Guaranteed delivery to all subscribers
22
+ - Ephemeral messages (remove after read)
23
+ - Guaranteed delivery to all regular subscribers
24
+ - Existing messages replayed to new regular subscribers
16
25
  - NATS-compatible API patterns
17
-
18
- ## Installation
19
-
20
- ```bash
21
- npm install queuebit
22
- ```
26
+ - Browser client support
23
27
 
24
28
  ## Documentation
25
29
 
@@ -27,250 +31,139 @@ npm install queuebit
27
31
  - **[API Reference](./docs/API.md)** - Complete API documentation
28
32
  - **[Examples](./docs/EXAMPLES.md)** - Practical examples for common use cases
29
33
 
30
- ## Usage
31
-
32
- ### Running the Server
34
+ ## Quick Start
33
35
 
34
- ```bash
35
- npm run server
36
- ```
36
+ ### Start the Server
37
37
 
38
- Or with custom options:
39
- ```bash
40
- node src/server-runner.js --port=4000 --max-queue=5000
38
+ ```javascript
39
+ const { QueueBitServer } = require('./src/server');
40
+ const server = new QueueBitServer({ port: 3333 });
41
41
  ```
42
42
 
43
43
  On Windows:
44
44
  ```cmd
45
- start-server.cmd
45
+ start_server.cmd
46
46
  ```
47
47
 
48
48
  ### Node.js Client
49
49
 
50
50
  ```javascript
51
- const { QueueBitClient } = require('queuebit');
51
+ const { QueueBitClient } = require('./src/client-node');
52
52
 
53
- const client = new QueueBitClient('http://localhost:3000');
53
+ const client = new QueueBitClient('http://localhost:3333');
54
54
 
55
55
  // Subscribe to messages
56
56
  await client.subscribe((message) => {
57
57
  console.log('Received:', message.data);
58
- });
58
+ }, { subject: 'events' });
59
59
 
60
60
  // Publish a message
61
- await client.publish({ hello: 'world' });
61
+ await client.publish({ hello: 'world' }, { subject: 'events' });
62
62
  ```
63
63
 
64
64
  ### Browser Client
65
65
 
66
- Include Socket.IO and QueueBit client in your HTML:
67
-
68
66
  ```html
69
67
  <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
-
68
+ <script src="src/client-browser.js"></script>
72
69
  <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!' });
70
+ const client = new QueueBitClient('http://localhost:3333');
71
+ client.subscribe((msg) => console.log('Received:', msg.data), { subject: 'events' });
72
+ client.publish({ hello: 'world from browser!' }, { subject: 'events' });
82
73
  </script>
83
74
  ```
84
75
 
85
- See `examples/browser-example.html` for a complete browser example.
76
+ See [`examples/qpanel.html`](./examples/qpanel.html) for a complete browser dashboard.
77
+ Open it with Live Server in VS Code to test.
86
78
 
87
- ### Server
79
+ ### In-Process (Server + Client in Same Process)
88
80
 
89
81
  ```javascript
90
- const { QueueBitServer } = require('queuebit');
91
-
92
- const server = new QueueBitServer({
93
- port: 3000,
94
- maxQueueSize: 10000
95
- });
96
- ```
82
+ const { QueueBitServer } = require('./src/server');
83
+ const { QueueBitClient } = require('./src/client-node');
97
84
 
98
- ### Client
85
+ const server = new QueueBitServer({ port: 3333 });
86
+ const client = new QueueBitClient('http://localhost:3333');
99
87
 
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
- });
88
+ setTimeout(async () => {
89
+ await client.subscribe((msg) => console.log('Got:', msg.data));
90
+ await client.publish({ hello: 'world' });
91
+ }, 500);
135
92
  ```
136
93
 
137
- ## Performance
138
-
139
- QueueBit is optimized for high throughput:
94
+ See [`examples/inprocessserver.js`](./examples/inprocessserver.js) for a full example.
140
95
 
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
96
+ ## API Overview
154
97
 
155
- ## API
98
+ ### Server Options
156
99
 
157
- ### QueueBitServer
100
+ | Option | Type | Default | Description |
101
+ |--------|------|---------|-------------|
102
+ | `port` | number | 3333 | Server port |
103
+ | `maxQueueSize` | number | 10000 | Max messages per subject |
158
104
 
159
- #### Constructor Options
160
- - `port` (number): Server port (default: 3000)
161
- - `maxQueueSize` (number): Maximum messages per subject (default: 10000)
105
+ ### Client Methods
162
106
 
163
- ### QueueBitClient
164
-
165
- #### Methods
166
-
167
- ##### `publish(message, options)`
107
+ #### `publish(message, options)`
168
108
  Publish a message to the queue.
169
109
 
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
110
+ | Option | Type | Description |
111
+ |--------|------|-------------|
112
+ | `subject` | string | Message subject/topic (default: `'default'`) |
113
+ | `expiry` | Date | Message expiration date |
114
+ | `removeAfterRead` | boolean | Ephemeral — remove after first delivery |
181
115
 
182
- ##### `unsubscribe(options)`
183
- Unsubscribe from messages.
116
+ #### `subscribe(callback, options)`
117
+ Subscribe to messages. Existing messages are replayed to new regular subscribers.
184
118
 
185
- ##### `disconnect()`
186
- Disconnect from the server.
119
+ | Option | Type | Description |
120
+ |--------|------|-------------|
121
+ | `subject` | string | Subscribe to specific subject (default: `'default'`) |
122
+ | `queue` | string | Unique name to join the load balancer with round-robin delivery |
187
123
 
188
- ## Publishing to NPM
124
+ #### `unsubscribe(options)` · `getMessages(options)` · `disconnect()`
189
125
 
190
- ### First Time Setup
126
+ See [API Reference](./docs/API.md) for full details.
191
127
 
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
128
+ ## Load Balancer
201
129
 
202
- ### Publishing
130
+ Each worker subscribes with a **unique `queue` name** and gets its own load balancer ID.
131
+ Messages are distributed round-robin — each message goes to exactly **one** worker.
203
132
 
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
- ```
133
+ ```javascript
134
+ // Worker 1 → LB#1
135
+ await worker1.subscribe((msg) => {
136
+ console.log(`LB#${msg.loadBalancerId}:`, msg.data);
137
+ }, { subject: 'tasks', queue: 'worker-1' });
241
138
 
242
- ### Publishing
139
+ // Worker 2 → LB#2
140
+ await worker2.subscribe((msg) => {
141
+ console.log(`LB#${msg.loadBalancerId}:`, msg.data);
142
+ }, { subject: 'tasks', queue: 'worker-2' });
243
143
 
244
- #### Update Version
245
- ```cmd
246
- update-version.cmd
144
+ // Publishes cycle: LB#1 → LB#2 → LB#1 → LB#2 ...
247
145
  ```
248
146
 
249
- #### Dry Run (test without publishing)
250
- ```cmd
251
- publish-dry-run.cmd
252
- ```
147
+ See [`examples/queuegroup.js`](./examples/queuegroup.js) for a runnable demo.
253
148
 
254
- #### Publish to NPM
255
- ```cmd
256
- publish.cmd
257
- ```
149
+ ## Examples
258
150
 
259
- Or manually:
260
- ```bash
261
- npm login
262
- npm test
263
- npm publish
264
- ```
151
+ | File | Description |
152
+ |------|-------------|
153
+ | [`examples/server2server.js`](./examples/server2server.js) | HTTP server using an external QueueBit server |
154
+ | [`examples/inprocessserver.js`](./examples/inprocessserver.js) | HTTP server with QueueBit running in-process |
155
+ | [`examples/queuegroup.js`](./examples/queuegroup.js) | Load balancer demo with 3 workers |
156
+ | [`examples/qpanel.html`](./examples/qpanel.html) | Browser dashboard |
157
+ | [`test/test-harness.js`](./test/test-harness.js) | Full test suite |
265
158
 
266
- ## Testing
159
+ ## Performance
267
160
 
268
- ```bash
269
- npm test
270
- ```
161
+ - **WebSocket-only transport** for reduced overhead
162
+ - **Batch message processing** (100 messages per batch)
163
+ - **Async delivery** via `setImmediate` to prevent blocking
164
+ - **No compression** for maximum speed
165
+ - **Typical throughput**: 20,000–50,000+ messages/second
271
166
 
272
167
  ## License
273
168
 
274
169
  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.