UNPKG

@tulioncds/nestjs-nats-streaming-transport

Version:

Fork Nats Streaming Transport for NestJS - Auto Reconnect stan

47 lines 2.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Listener = void 0; const microservices_1 = require("@nestjs/microservices"); const nats_streaming_context_1 = require("./nats-streaming.context"); const create_stan_connection_1 = require("./utils/create-stan-connection"); const build_subscription_options_1 = require("./utils/build-subscription-options"); const parse_message_1 = require("./utils/parse-message"); class Listener extends microservices_1.Server { constructor(clusterId, clientId, queueGroup, connectOptions, subscriptionOptions) { super(); this.clusterId = clusterId; this.clientId = clientId; this.queueGroup = queueGroup; this.connectOptions = connectOptions; this.subscriptionOptions = subscriptionOptions; } async listen(callback) { this.logger.log('Setting up event listeners...'); this.connection = await (0, create_stan_connection_1.createConnection)(this.clusterId, this.clientId, this.connectOptions, this.bindEventHandlers.bind(this)); this.bindEventHandlers(); callback(); } close() { this.connection.close(); } async bindEventHandlers() { const registerdPatterns = Array.from(this.messageHandlers.keys()); if (!registerdPatterns) { this.logger.log('No message handlers registered'); } registerdPatterns.forEach((subject) => { const options = (0, build_subscription_options_1.buildSubscriptionOptions)(this.subscriptionOptions, this.connection); const subscription = this.connection.subscribe(subject, this.queueGroup, options); subscription.on('message', async (msg) => { const handler = this.getHandlerByPattern(subject); const data = (0, parse_message_1.parseMessage)(msg); const context = new nats_streaming_context_1.NatsStreamingContext([msg]); const stream = this.transformToObservable(await handler(data, context)); this.send(stream, () => null); }); this.logger.log(`Subscribed to ${subject}`); }); } } exports.Listener = Listener; //# sourceMappingURL=listener.js.map