@opens/rabbitmq 1.0.0 → 1.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 +100 -0
- package/dist/index.d.ts +39 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# OpensMQ
|
|
2
|
+
|
|
3
|
+
This library is a lightweight wrapper around RabbitMQ that simplifies publishing and subscribing to messages using `amqp-connection-manager` and `amqplib`. This library provides an easy-to-use API for event-driven applications.
|
|
4
|
+
|
|
5
|
+
## Getting Started
|
|
6
|
+
|
|
7
|
+
### Initialization
|
|
8
|
+
|
|
9
|
+
To start using `PubSubMQ`, create an instance and initialize the connection to RabbitMQ. This must be done first:
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
import { pubsub } from './rabbitmq';
|
|
13
|
+
|
|
14
|
+
pubsub.start({
|
|
15
|
+
hostname: 'localhost',
|
|
16
|
+
port: 5672,
|
|
17
|
+
username: 'guest',
|
|
18
|
+
password: 'guest',
|
|
19
|
+
protocol: 'amqp',
|
|
20
|
+
});
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Publishing Messages
|
|
24
|
+
|
|
25
|
+
You can publish messages to a specific exchange with a routing key:
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
await pubsub.publish('my-exchange', 'my-routing-key', { message: 'Hello, World!' });
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Subscribing to Messages
|
|
32
|
+
|
|
33
|
+
To subscribe to a queue and process messages:
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
pubsub.subscribe({
|
|
37
|
+
name: 'my-queue',
|
|
38
|
+
exchange: 'my-exchange',
|
|
39
|
+
bindingKey: 'my-routing-key',
|
|
40
|
+
durable: true,
|
|
41
|
+
prefetch: 1,
|
|
42
|
+
up: async () => console.log('Consumer is up'),
|
|
43
|
+
down: async () => console.log('Consumer is down'),
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Handling Message Acknowledgments
|
|
48
|
+
|
|
49
|
+
The library automatically acknowledges messages if processed successfully. If an error occurs, the message is rejected (`nack`).
|
|
50
|
+
|
|
51
|
+
## Advanced Configuration
|
|
52
|
+
|
|
53
|
+
### Setting TTL (Time-To-Live)
|
|
54
|
+
|
|
55
|
+
To set a TTL for messages in the queue:
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
pubsub.subscribe({
|
|
59
|
+
name: 'expiring-queue',
|
|
60
|
+
exchange: 'my-exchange',
|
|
61
|
+
bindingKey: 'expiring-key',
|
|
62
|
+
ttl: 30000, // Messages expire after 30 seconds
|
|
63
|
+
consumer: async (data) => {
|
|
64
|
+
console.log('Processing expiring message:', data);
|
|
65
|
+
},
|
|
66
|
+
up: async () => console.log('Consumer is up'),
|
|
67
|
+
down: async () => console.log('Consumer is down'),
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Exclusive Queues
|
|
72
|
+
|
|
73
|
+
For a queue that only allows a single connection:
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
pubsub.subscribe({
|
|
77
|
+
name: 'exclusive-queue',
|
|
78
|
+
exchange: 'my-exchange',
|
|
79
|
+
bindingKey: 'exclusive-key',
|
|
80
|
+
exclusive: true,
|
|
81
|
+
consumer: async (data) => {
|
|
82
|
+
console.log('Processing exclusive message:', data);
|
|
83
|
+
},
|
|
84
|
+
up: async () => console.log('Consumer is up'),
|
|
85
|
+
down: async () => console.log('Consumer is down'),
|
|
86
|
+
});
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Error Handling
|
|
90
|
+
|
|
91
|
+
Errors in the consumer function cause messages to be rejected (`nack`). Ensure error handling is implemented inside your consumer to prevent message loss.
|
|
92
|
+
|
|
93
|
+
## Contributing
|
|
94
|
+
|
|
95
|
+
Feel free to contribute by submitting issues or pull requests.
|
|
96
|
+
|
|
97
|
+
## License
|
|
98
|
+
|
|
99
|
+
This project is licensed under the MIT License.
|
|
100
|
+
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { connect, ChannelWrapper } from 'amqp-connection-manager';
|
|
2
|
+
import { MessageFields, MessageProperties } from 'amqplib';
|
|
3
|
+
|
|
4
|
+
type ConsumerFunc = (data: any, metadata: {
|
|
5
|
+
fields: MessageFields;
|
|
6
|
+
properties: MessageProperties;
|
|
7
|
+
}) => Promise<void>;
|
|
8
|
+
interface SubscribeParams {
|
|
9
|
+
ttl?: number;
|
|
10
|
+
name: string;
|
|
11
|
+
exchange: string;
|
|
12
|
+
durable?: boolean;
|
|
13
|
+
prefetch?: number;
|
|
14
|
+
queueArguments?: Record<string, any>;
|
|
15
|
+
exclusive?: boolean;
|
|
16
|
+
bindingKey: string;
|
|
17
|
+
consumer?: ConsumerFunc;
|
|
18
|
+
up?: ConsumerFunc;
|
|
19
|
+
down?: ConsumerFunc;
|
|
20
|
+
}
|
|
21
|
+
declare class PubSubMQ {
|
|
22
|
+
private startConnection;
|
|
23
|
+
private connection;
|
|
24
|
+
private publisherChannel;
|
|
25
|
+
constructor(startConnection: typeof connect);
|
|
26
|
+
start(params: {
|
|
27
|
+
hostname: string;
|
|
28
|
+
password: string;
|
|
29
|
+
protocol: string;
|
|
30
|
+
username: string;
|
|
31
|
+
port: number;
|
|
32
|
+
}): void;
|
|
33
|
+
publish(exchange: string, routingKey: string, payload: any, options?: Object): Promise<boolean>;
|
|
34
|
+
subscribe(params: SubscribeParams): ChannelWrapper;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
declare const pubsub: PubSubMQ;
|
|
38
|
+
|
|
39
|
+
export { pubsub };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opens/rabbitmq",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "A wrapper around common message brokers",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"dist"
|
|
10
10
|
],
|
|
11
11
|
"scripts": {
|
|
12
|
-
"build": "tsup",
|
|
12
|
+
"build": "tsup --dts",
|
|
13
13
|
"dev": "tsup --watch",
|
|
14
14
|
"test": "vitest run",
|
|
15
15
|
"test:watch": "vitest"
|