UNPKG

moleculer

Version:

Fast & powerful microservices framework for Node.JS

148 lines (127 loc) 3.08 kB
/* * moleculer * Copyright (c) 2017 Ice Services (https://github.com/ice-services/moleculer) * MIT Licensed */ "use strict"; const Promise = require("bluebird"); const Transporter = require("./base"); /** * Transporter for NATS * * More info: http://nats.io/ * * @class NatsTransporter * @extends {Transporter} */ class NatsTransporter extends Transporter { /** * Creates an instance of NatsTransporter. * * @param {any} opts * * @memberOf NatsTransporter */ constructor(opts) { super(opts); if (typeof this.opts == "string") this.opts = { nats: this.opts }; this.client = null; } /** * Connect to a NATS server * * @memberOf NatsTransporter */ connect() { return new Promise((resolve, reject) => { let Nats; try { Nats = require("nats"); } catch(err) { /* istanbul ignore next */ this.broker.fatal("The 'nats' package is missing! Please install it with 'npm install nats --save' command!", err, true); } const client = Nats.connect(this.opts.nats); this._client = client; // For tests client.on("connect", () => { this.client = client; this.logger.info("NATS connected!"); this.onConnected().then(resolve); }); /* istanbul ignore next */ client.on("reconnect", () => { this.logger.info("NATS reconnected!"); this.onConnected(true); }); /* istanbul ignore next */ client.on("reconnecting", () => { this.logger.warn("NATS reconnecting..."); }); /* istanbul ignore next */ client.on("disconnect", () => { if (this.connected) { this.logger.warn("NATS disconnected!"); this.connected = false; } }); /* istanbul ignore next */ client.on("error", e => { this.logger.error("NATS error!", e.message); if (!client.connected) reject(e); }); /* istanbul ignore next */ client.on("close", () => { this.connected = false; this.logger.warn("NATS connection closed!"); }); }); } /** * Disconnect from a NATS server * * @memberOf NatsTransporter */ disconnect() { if (this.client) { this.client.close(); this.client = null; } } /** * Reconnect to server after x seconds * * @memberOf BaseTransporter */ /*reconnectAfterTime() { //this.logger.info("Reconnecting after 5 sec..."); setTimeout(() => { this.connect(); }, 5 * 1000); }*/ /** * Subscribe to a command * * @param {String} cmd * @param {String} nodeID * * @memberOf NatsTransporter */ subscribe(cmd, nodeID) { const t = this.getTopicName(cmd, nodeID); this.client.subscribe(t, (msg) => this.messageHandler(cmd, msg)); } /** * Publish a packet * * @param {Packet} packet * * @memberOf NatsTransporter */ publish(packet) { const data = packet.serialize(); this.client.publish(this.getTopicName(packet.type, packet.target), data); } } module.exports = NatsTransporter;