@usermetrics/queuebit 1.0.1 → 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,26 +1,29 @@
1
1
  # QueueBit
2
2
 
3
3
  A high performance socket-based message queue server with guaranteed delivery, compatible with NATS queue patterns.
4
- Built in Load Balancer. (see examples).
4
+ Built-in Load Balancer with round-robin delivery. (see examples).
5
5
 
6
- It can run in-process in an existing nodejs app, separately as a nodejs server, or run clients
6
+ It can run in-process in an existing Node.js app, separately as a standalone server, or run clients
7
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
+ ```
14
+
8
15
 
9
16
  ## Features
10
17
 
11
18
  - WebSocket-based message queue
12
19
  - Subject-based message routing
13
- - Load-balancer
20
+ - Load balancer with round-robin delivery (each message goes to exactly one worker)
14
21
  - Message expiry support
15
- - Remove after read (ephemeral messages)
16
- - 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
17
25
  - NATS-compatible API patterns
18
-
19
- ## Installation
20
-
21
- ```bash
22
- npm install queuebit
23
- ```
26
+ - Browser client support
24
27
 
25
28
  ## Documentation
26
29
 
@@ -28,168 +31,139 @@ npm install queuebit
28
31
  - **[API Reference](./docs/API.md)** - Complete API documentation
29
32
  - **[Examples](./docs/EXAMPLES.md)** - Practical examples for common use cases
30
33
 
31
- ## Usage
34
+ ## Quick Start
32
35
 
33
- ### Running the Server
36
+ ### Start the Server
34
37
 
35
- ```bash
36
- npm run server
37
- ```
38
-
39
- Or with custom options:
40
- ```bash
41
- 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 });
42
41
  ```
43
42
 
44
43
  On Windows:
45
44
  ```cmd
46
- start-server.cmd
45
+ start_server.cmd
47
46
  ```
48
47
 
49
48
  ### Node.js Client
50
49
 
51
50
  ```javascript
52
- const { QueueBitClient } = require('queuebit');
51
+ const { QueueBitClient } = require('./src/client-node');
53
52
 
54
- const client = new QueueBitClient('http://localhost:3000');
53
+ const client = new QueueBitClient('http://localhost:3333');
55
54
 
56
55
  // Subscribe to messages
57
56
  await client.subscribe((message) => {
58
57
  console.log('Received:', message.data);
59
- });
58
+ }, { subject: 'events' });
60
59
 
61
60
  // Publish a message
62
- await client.publish({ hello: 'world' });
61
+ await client.publish({ hello: 'world' }, { subject: 'events' });
63
62
  ```
64
63
 
65
64
  ### Browser Client
66
65
 
67
- Include Socket.IO and QueueBit client in your HTML:
68
-
69
66
  ```html
70
67
  <script src="https://cdn.socket.io/4.7.2/socket.io.min.js"></script>
71
- <script src="node_modules/queuebit/src/client-browser.js"></script>
72
-
68
+ <script src="src/client-browser.js"></script>
73
69
  <script>
74
- const client = new QueueBitClient('http://localhost:3000');
75
-
76
- // Subscribe to messages
77
- client.subscribe((message) => {
78
- console.log('Received:', message.data);
79
- });
80
-
81
- // Publish a message
82
- 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' });
83
73
  </script>
84
74
  ```
85
75
 
86
- See `examples/qpanel.html` for a complete browser example.
87
- Open it with Live Server in vscode to test.
88
-
89
- ### Server
90
-
91
- ```javascript
92
- const { QueueBitServer } = require('queuebit');
93
-
94
- const server = new QueueBitServer({
95
- port: 3000,
96
- maxQueueSize: 10000
97
- });
98
- ```
76
+ See [`examples/qpanel.html`](./examples/qpanel.html) for a complete browser dashboard.
77
+ Open it with Live Server in VS Code to test.
99
78
 
100
- ### Client
79
+ ### In-Process (Server + Client in Same Process)
101
80
 
102
81
  ```javascript
103
- const { QueueBitClient } = require('queuebit');
82
+ const { QueueBitServer } = require('./src/server');
83
+ const { QueueBitClient } = require('./src/client-node');
104
84
 
105
- const client = new QueueBitClient('http://localhost:3000');
85
+ const server = new QueueBitServer({ port: 3333 });
86
+ const client = new QueueBitClient('http://localhost:3333');
106
87
 
107
- // Subscribe to messages
108
- await client.subscribe((message) => {
109
- console.log('Received:', message.data);
110
- });
88
+ setTimeout(async () => {
89
+ await client.subscribe((msg) => console.log('Got:', msg.data));
90
+ await client.publish({ hello: 'world' });
91
+ }, 500);
92
+ ```
111
93
 
112
- // Publish a message
113
- await client.publish({ hello: 'world' });
94
+ See [`examples/inprocessserver.js`](./examples/inprocessserver.js) for a full example.
114
95
 
115
- // Subject-based routing
116
- await client.subscribe((message) => {
117
- console.log('Order:', message.data);
118
- }, { subject: 'orders' });
96
+ ## API Overview
119
97
 
120
- await client.publish({ orderId: 123 }, { subject: 'orders' });
98
+ ### Server Options
121
99
 
122
- // Queue groups (load balanced)
123
- await client.subscribe((message) => {
124
- console.log('Worker received:', message.data);
125
- }, { subject: 'tasks', queue: 'workers' });
126
-
127
- // Message with expiry
128
- const expiryDate = new Date(Date.now() + 60000); // 1 minute
129
- await client.publish({ data: 'expires soon' }, {
130
- expiry: expiryDate
131
- });
132
-
133
- // One-time read message
134
- await client.publish({ data: 'read once' }, {
135
- removeAfterRead: true
136
- });
137
- ```
100
+ | Option | Type | Default | Description |
101
+ |--------|------|---------|-------------|
102
+ | `port` | number | 3333 | Server port |
103
+ | `maxQueueSize` | number | 10000 | Max messages per subject |
138
104
 
139
- ## Performance
105
+ ### Client Methods
140
106
 
141
- QueueBit is optimized for high throughput:
107
+ #### `publish(message, options)`
108
+ Publish a message to the queue.
142
109
 
143
- - **WebSocket-only transport** for reduced overhead
144
- - **Batch message processing** on the server
145
- - **Async delivery** to prevent blocking
146
- - **No compression** for maximum speed
147
- - **Typical throughput**: 20,000-50,000+ messages/second (depends on hardware and message size)
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 |
148
115
 
149
- ### Performance Tips
116
+ #### `subscribe(callback, options)`
117
+ Subscribe to messages. Existing messages are replayed to new regular subscribers.
150
118
 
151
- 1. Use WebSocket transport only (default)
152
- 2. Send messages in batches when possible
153
- 3. Avoid very large message payloads
154
- 4. Use queue groups for load balancing across multiple consumers
155
- 5. Monitor queue sizes to prevent memory issues
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 |
156
123
 
157
- ## API
124
+ #### `unsubscribe(options)` · `getMessages(options)` · `disconnect()`
158
125
 
159
- ### QueueBitServer
126
+ See [API Reference](./docs/API.md) for full details.
160
127
 
161
- #### Constructor Options
162
- - `port` (number): Server port (default: 3000)
163
- - `maxQueueSize` (number): Maximum messages per subject (default: 10000)
128
+ ## Load Balancer
164
129
 
165
- ### QueueBitClient
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.
166
132
 
167
- #### Methods
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' });
168
138
 
169
- ##### `publish(message, options)`
170
- Publish a message to the queue.
139
+ // Worker 2 → LB#2
140
+ await worker2.subscribe((msg) => {
141
+ console.log(`LB#${msg.loadBalancerId}:`, msg.data);
142
+ }, { subject: 'tasks', queue: 'worker-2' });
171
143
 
172
- Options:
173
- - `subject` (string): Message subject/topic
174
- - `expiry` (Date): Message expiration date
175
- - `removeAfterRead` (boolean): Remove message after first delivery
144
+ // Publishes cycle: LB#1 → LB#2 → LB#1 → LB#2 ...
145
+ ```
176
146
 
177
- ##### `subscribe(callback, options)`
178
- Subscribe to messages.
147
+ See [`examples/queuegroup.js`](./examples/queuegroup.js) for a runnable demo.
179
148
 
180
- Options:
181
- - `subject` (string): Subscribe to specific subject
182
- - `queue` (string): Join a queue group for load-balanced delivery
149
+ ## Examples
183
150
 
184
- ##### `unsubscribe(options)`
185
- Unsubscribe from messages.
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 |
186
158
 
187
- ##### `disconnect()`
188
- Disconnect from the server.
159
+ ## Performance
189
160
 
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
190
166
 
191
167
  ## License
192
168
 
193
169
  MIT License - See [LICENSE](LICENSE) file for details.
194
-
195
- 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.