@mulingai-npm/message-broker 1.1.1 → 1.1.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.
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { RabbitMQClient } from '../rabbitmq-client';
|
|
2
|
-
import { IMulingstreamListenerFeedData } from '@mulingai-npm/web-sockets';
|
|
3
|
-
export declare class MulingstreamListenerFeed {
|
|
4
|
-
private client;
|
|
5
|
-
constructor(client: RabbitMQClient);
|
|
6
|
-
initialize(): Promise<void>;
|
|
7
|
-
publish(data: IMulingstreamListenerFeedData): Promise<void>;
|
|
8
|
-
subscribe(queueName: string, onMessage: (data: IMulingstreamListenerFeedData) => Promise<void> | void): Promise<void>;
|
|
9
|
-
}
|
|
1
|
+
import { RabbitMQClient } from '../rabbitmq-client';
|
|
2
|
+
import { IMulingstreamListenerFeedData } from '@mulingai-npm/web-sockets';
|
|
3
|
+
export declare class MulingstreamListenerFeed {
|
|
4
|
+
private client;
|
|
5
|
+
constructor(client: RabbitMQClient);
|
|
6
|
+
initialize(): Promise<void>;
|
|
7
|
+
publish(data: IMulingstreamListenerFeedData): Promise<void>;
|
|
8
|
+
subscribe(queueName: string, onMessage: (data: IMulingstreamListenerFeedData) => Promise<void> | void): Promise<void>;
|
|
9
|
+
}
|
|
@@ -1,44 +1,42 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MulingstreamListenerFeed = void 0;
|
|
4
|
-
const EXCHANGE_NAME = 'mulingstream-listener-feed';
|
|
5
|
-
const EXCHANGE_TYPE = 'fanout';
|
|
6
|
-
class MulingstreamListenerFeed {
|
|
7
|
-
constructor(client) {
|
|
8
|
-
this.client = client;
|
|
9
|
-
}
|
|
10
|
-
async initialize() {
|
|
11
|
-
const channel = this.client.getChannelOrThrow();
|
|
12
|
-
await channel.assertExchange(EXCHANGE_NAME, EXCHANGE_TYPE, {
|
|
13
|
-
durable: false
|
|
14
|
-
});
|
|
15
|
-
console.log(`MulingstreamListenerFeed exchange '${EXCHANGE_NAME}' asserted.`);
|
|
16
|
-
}
|
|
17
|
-
async publish(data) {
|
|
18
|
-
const channel = this.client.getChannelOrThrow();
|
|
19
|
-
const payload = Buffer.from(JSON.stringify(data));
|
|
20
|
-
channel.publish(EXCHANGE_NAME, '', payload); // With a fanout exchange, the routingKey is usually '' (ignored).
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
await channel.
|
|
26
|
-
await channel.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
exports.MulingstreamListenerFeed = MulingstreamListenerFeed;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MulingstreamListenerFeed = void 0;
|
|
4
|
+
const EXCHANGE_NAME = 'mulingstream-listener-feed';
|
|
5
|
+
const EXCHANGE_TYPE = 'fanout';
|
|
6
|
+
class MulingstreamListenerFeed {
|
|
7
|
+
constructor(client) {
|
|
8
|
+
this.client = client;
|
|
9
|
+
}
|
|
10
|
+
async initialize() {
|
|
11
|
+
const channel = this.client.getChannelOrThrow();
|
|
12
|
+
await channel.assertExchange(EXCHANGE_NAME, EXCHANGE_TYPE, {
|
|
13
|
+
durable: false
|
|
14
|
+
});
|
|
15
|
+
console.log(`MulingstreamListenerFeed exchange '${EXCHANGE_NAME}' asserted.`);
|
|
16
|
+
}
|
|
17
|
+
async publish(data) {
|
|
18
|
+
const channel = this.client.getChannelOrThrow();
|
|
19
|
+
const payload = Buffer.from(JSON.stringify(data));
|
|
20
|
+
channel.publish(EXCHANGE_NAME, '', payload); // With a fanout exchange, the routingKey is usually '' (ignored).
|
|
21
|
+
}
|
|
22
|
+
async subscribe(queueName, onMessage) {
|
|
23
|
+
const channel = this.client.getChannelOrThrow();
|
|
24
|
+
await channel.assertQueue(queueName, { durable: true });
|
|
25
|
+
await channel.bindQueue(queueName, EXCHANGE_NAME, '');
|
|
26
|
+
await channel.consume(queueName, async (msg) => {
|
|
27
|
+
if (!msg) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
const data = JSON.parse(msg.content.toString());
|
|
32
|
+
await onMessage(data);
|
|
33
|
+
channel.ack(msg);
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
console.error(`Error in MulingstreamListenerFeed subscription queue ${queueName}:`, error);
|
|
37
|
+
channel.nack(msg);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.MulingstreamListenerFeed = MulingstreamListenerFeed;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { Channel } from 'amqplib';
|
|
2
|
-
export declare class RabbitMQClient {
|
|
3
|
-
private url;
|
|
4
|
-
private connection;
|
|
5
|
-
private channel;
|
|
6
|
-
constructor(rabbitUrl: string);
|
|
7
|
-
connect(): Promise<void>;
|
|
8
|
-
close(): Promise<void>;
|
|
9
|
-
getChannelOrThrow(): Channel;
|
|
10
|
-
}
|
|
1
|
+
import { Channel } from 'amqplib';
|
|
2
|
+
export declare class RabbitMQClient {
|
|
3
|
+
private url;
|
|
4
|
+
private connection;
|
|
5
|
+
private channel;
|
|
6
|
+
constructor(rabbitUrl: string);
|
|
7
|
+
connect(): Promise<void>;
|
|
8
|
+
close(): Promise<void>;
|
|
9
|
+
getChannelOrThrow(): Channel;
|
|
10
|
+
}
|
package/dist/rabbitmq-client.js
CHANGED
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RabbitMQClient = void 0;
|
|
4
|
-
const amqplib_1 = require("amqplib");
|
|
5
|
-
class RabbitMQClient {
|
|
6
|
-
constructor(rabbitUrl) {
|
|
7
|
-
this.connection = null;
|
|
8
|
-
this.channel = null;
|
|
9
|
-
this.url = rabbitUrl;
|
|
10
|
-
}
|
|
11
|
-
async connect() {
|
|
12
|
-
if (this.connection) {
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
this.connection = await (0, amqplib_1.connect)(this.url);
|
|
16
|
-
this.channel = await this.connection.createChannel();
|
|
17
|
-
console.log('RabbitMQ connected and channel created.');
|
|
18
|
-
// Cleanup on close
|
|
19
|
-
this.connection.on('close', () => {
|
|
20
|
-
console.warn('RabbitMQ connection closed.');
|
|
21
|
-
this.connection = null;
|
|
22
|
-
this.channel = null;
|
|
23
|
-
});
|
|
24
|
-
this.connection.on('error', (err) => {
|
|
25
|
-
console.error('RabbitMQ connection error:', err);
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
async close() {
|
|
29
|
-
if (this.channel) {
|
|
30
|
-
await this.channel.close();
|
|
31
|
-
this.channel = null;
|
|
32
|
-
}
|
|
33
|
-
if (this.connection) {
|
|
34
|
-
await this.connection.close();
|
|
35
|
-
this.connection = null;
|
|
36
|
-
}
|
|
37
|
-
console.log('RabbitMQ connection closed.');
|
|
38
|
-
}
|
|
39
|
-
getChannelOrThrow() {
|
|
40
|
-
if (!this.channel) {
|
|
41
|
-
throw new Error('RabbitMQ channel not initialized. Call connect() first.');
|
|
42
|
-
}
|
|
43
|
-
return this.channel;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
exports.RabbitMQClient = RabbitMQClient;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RabbitMQClient = void 0;
|
|
4
|
+
const amqplib_1 = require("amqplib");
|
|
5
|
+
class RabbitMQClient {
|
|
6
|
+
constructor(rabbitUrl) {
|
|
7
|
+
this.connection = null;
|
|
8
|
+
this.channel = null;
|
|
9
|
+
this.url = rabbitUrl;
|
|
10
|
+
}
|
|
11
|
+
async connect() {
|
|
12
|
+
if (this.connection) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
this.connection = await (0, amqplib_1.connect)(this.url);
|
|
16
|
+
this.channel = await this.connection.createChannel();
|
|
17
|
+
console.log('RabbitMQ connected and channel created.');
|
|
18
|
+
// Cleanup on close
|
|
19
|
+
this.connection.on('close', () => {
|
|
20
|
+
console.warn('RabbitMQ connection closed.');
|
|
21
|
+
this.connection = null;
|
|
22
|
+
this.channel = null;
|
|
23
|
+
});
|
|
24
|
+
this.connection.on('error', (err) => {
|
|
25
|
+
console.error('RabbitMQ connection error:', err);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
async close() {
|
|
29
|
+
if (this.channel) {
|
|
30
|
+
await this.channel.close();
|
|
31
|
+
this.channel = null;
|
|
32
|
+
}
|
|
33
|
+
if (this.connection) {
|
|
34
|
+
await this.connection.close();
|
|
35
|
+
this.connection = null;
|
|
36
|
+
}
|
|
37
|
+
console.log('RabbitMQ connection closed.');
|
|
38
|
+
}
|
|
39
|
+
getChannelOrThrow() {
|
|
40
|
+
if (!this.channel) {
|
|
41
|
+
throw new Error('RabbitMQ channel not initialized. Call connect() first.');
|
|
42
|
+
}
|
|
43
|
+
return this.channel;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.RabbitMQClient = RabbitMQClient;
|
package/package.json
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@mulingai-npm/message-broker",
|
|
3
|
-
"version": "1.1.
|
|
4
|
-
"main": "dist/index.js",
|
|
5
|
-
"types": "dist/index.d.ts",
|
|
6
|
-
"repository": {
|
|
7
|
-
"type": "git",
|
|
8
|
-
"url": "https://github.com/mulingai/mulingai-backend.git"
|
|
9
|
-
},
|
|
10
|
-
"publishConfig": {
|
|
11
|
-
"registry": "https://registry.npmjs.org/"
|
|
12
|
-
},
|
|
13
|
-
"private": false,
|
|
14
|
-
"scripts": {
|
|
15
|
-
"dev": "rm -f tsconfig.tsbuildinfo && tsc --watch",
|
|
16
|
-
"build": "rm -f tsconfig.tsbuildinfo && tsc",
|
|
17
|
-
"prepublishOnly": "npm run build"
|
|
18
|
-
},
|
|
19
|
-
"dependencies": {
|
|
20
|
-
"@mulingai-npm/web-sockets": "^1.3.1",
|
|
21
|
-
"amqplib": "^0.10.5"
|
|
22
|
-
},
|
|
23
|
-
"devDependencies": {
|
|
24
|
-
"concurrently": "^9.1.2",
|
|
25
|
-
"nodemon": "^3.1.9",
|
|
26
|
-
"typescript": "^4.9.5"
|
|
27
|
-
},
|
|
28
|
-
"files": [
|
|
29
|
-
"dist",
|
|
30
|
-
"package.json",
|
|
31
|
-
"README.md"
|
|
32
|
-
]
|
|
33
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@mulingai-npm/message-broker",
|
|
3
|
+
"version": "1.1.2",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/mulingai/mulingai-backend.git"
|
|
9
|
+
},
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"registry": "https://registry.npmjs.org/"
|
|
12
|
+
},
|
|
13
|
+
"private": false,
|
|
14
|
+
"scripts": {
|
|
15
|
+
"dev": "rm -f tsconfig.tsbuildinfo && tsc --watch",
|
|
16
|
+
"build": "rm -f tsconfig.tsbuildinfo && tsc",
|
|
17
|
+
"prepublishOnly": "npm run build"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@mulingai-npm/web-sockets": "^1.3.1",
|
|
21
|
+
"amqplib": "^0.10.5"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"concurrently": "^9.1.2",
|
|
25
|
+
"nodemon": "^3.1.9",
|
|
26
|
+
"typescript": "^4.9.5"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"package.json",
|
|
31
|
+
"README.md"
|
|
32
|
+
]
|
|
33
|
+
}
|