@mulingai-npm/message-broker 1.0.1

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.
@@ -0,0 +1,8 @@
1
+ import { RabbitMQClient } from '../rabbitmq-client';
2
+ export declare class TtsManager {
3
+ private client;
4
+ constructor(client: RabbitMQClient);
5
+ initialize(): Promise<void>;
6
+ publishTtsCompleted(data: any): Promise<void>;
7
+ subscribeTtsCompleted(queueName: string, onMessage: (data: any) => Promise<void> | void): Promise<void>;
8
+ }
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TtsManager = void 0;
4
+ const TTS_EXCHANGE = 'tts-exchange';
5
+ const TTS_EXCHANGE_TYPE = 'fanout'; // fanout means every queue bound to it gets every message.
6
+ class TtsManager {
7
+ constructor(client) {
8
+ this.client = client;
9
+ }
10
+ async initialize() {
11
+ const channel = this.client.getChannelOrThrow();
12
+ await channel.assertExchange(TTS_EXCHANGE, TTS_EXCHANGE_TYPE, {
13
+ durable: false
14
+ });
15
+ console.log(`TTS exchange '${TTS_EXCHANGE}' asserted.`);
16
+ }
17
+ async publishTtsCompleted(data) {
18
+ const channel = this.client.getChannelOrThrow();
19
+ const payload = Buffer.from(JSON.stringify(data));
20
+ // With a fanout exchange, the routingKey is usually '' (ignored).
21
+ channel.publish(TTS_EXCHANGE, '', payload);
22
+ console.log('Published TTS completed event:', data);
23
+ }
24
+ async subscribeTtsCompleted(queueName, onMessage) {
25
+ const channel = this.client.getChannelOrThrow();
26
+ // 1) Assert a queue (unique name for each microservice).
27
+ await channel.assertQueue(queueName, { durable: true });
28
+ // 2) Bind the queue to the TTS exchange. (Fanout -> routingKey usually ignored.)
29
+ await channel.bindQueue(queueName, TTS_EXCHANGE, '');
30
+ // 3) Consume messages:
31
+ await channel.consume(queueName, async (msg) => {
32
+ if (!msg) {
33
+ return;
34
+ }
35
+ try {
36
+ const data = JSON.parse(msg.content.toString());
37
+ await onMessage(data);
38
+ channel.ack(msg);
39
+ }
40
+ catch (error) {
41
+ console.error(`Error in TTS subscription queue ${queueName}:`, error);
42
+ channel.nack(msg);
43
+ }
44
+ });
45
+ console.log(`Subscribed to TTS completed events on queue "${queueName}".`);
46
+ }
47
+ }
48
+ exports.TtsManager = TtsManager;
@@ -0,0 +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
+ }
@@ -0,0 +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;
@@ -0,0 +1,14 @@
1
+ // import { RabbitMQClient } from '@mulingai-npm/message-broker/dist/rabbitmq-client';
2
+ // import { TtsManager } from '@mulingai-npm/message-broker/dist/tts-manager';
3
+ // const rabbitUrl = process.env.RABBIT_MQ_URL || 'amqp://guest:guest@localhost:5672';
4
+ // const rabbitClient = new RabbitMQClient(rabbitUrl);
5
+ // (async () => {
6
+ // await rabbitClient.connect();
7
+ // const ttsManager = new TtsManager(rabbitClient);
8
+ // await ttsManager.initialize();
9
+ // // Each subscribing service uses a unique queue name so they each get the message.
10
+ // await ttsManager.subscribeTtsCompleted('my-queue-other-service', async (msgData) => {
11
+ // console.log('Other service got TTS completed event:', msgData);
12
+ // // do something with msgData (e.g., store in DB, call another service, etc.)
13
+ // });
14
+ // })();
@@ -0,0 +1,17 @@
1
+ // import { RabbitMQClient } from '@mulingai-npm/message-broker/dist/rabbitmq-client';
2
+ // import { TtsManager } from '@mulingai-npm/message-broker/dist/tts-manager';
3
+ // // 1) Create the RabbitMQ client with your single URL
4
+ // const rabbitUrl = process.env.RABBIT_MQ_URL || 'amqp://guest:guest@localhost:5672';
5
+ // const rabbitClient = new RabbitMQClient(rabbitUrl);
6
+ // (async () => {
7
+ // // 2) Connect
8
+ // await rabbitClient.connect();
9
+ // // 3) Initialize the TTS manager
10
+ // const ttsManager = new TtsManager(rabbitClient);
11
+ // await ttsManager.initialize();
12
+ // // 4) Suppose your TTS logic finishes some job, then:
13
+ // // e.g., after processing text into audio, you publish an event
14
+ // const resultData = { jobId: '123', audioUrl: 'https://my-bucket/xyz.wav' };
15
+ // await ttsManager.publishTtsCompleted(resultData);
16
+ // // 5) Keep your service running; handle more TTS requests...
17
+ // })();
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@mulingai-npm/message-broker",
3
+ "version": "1.0.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
+ "amqplib": "^0.10.5"
21
+ },
22
+ "devDependencies": {
23
+ "concurrently": "^9.1.2",
24
+ "nodemon": "^3.1.9",
25
+ "typescript": "^4.9.5"
26
+ },
27
+ "files": [
28
+ "dist",
29
+ "package.json",
30
+ "README.md"
31
+ ]
32
+ }