/** * Subscribe to balanced action commands * * @param {String} action * @memberof NatsTransporter */ subscribeBalancedRequest(action) { const topic = `${this.prefix}.${PACKET_REQUEST}B.${action}`; const queue = action; this.subscriptions.push(this.client.subscribe(topic, { queue }, (msg) => this.receive(PACKET_REQUEST, msg))); }
/** * Subscribe to balanced event command * * @param {String} event * @param {String} group * @memberof NatsTransporter */ subscribeBalancedEvent(event, group) { const topic = `${this.prefix}.${PACKET_EVENT}B.${group}.${event}`.replace(/\*\*.*$/g, ">"); this.subscriptions.push(this.client.subscribe(topic, { queue: group }, (msg) => this.receive(PACKET_EVENT, msg))); }
nats1.subscribe("MOL.EVENTB.group.test.event", /*{ queue: "group" },*/ message => { broker.logger.info(kleur.green().bold("nodejs-subscriber1: Test event received with payload:"), message); });
/** * 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.receive(cmd, msg)); return this.broker.Promise.resolve(); }
let start = new Date(); let sid = nc1.subscribe("test", function () { received += 1; let start = new Date(); nc1.subscribe("ping", msg => { nc1.publish("pong", "ok"); }); }; nc2.subscribe("pong", () => { received += 1; let start = new Date(); let sid = nc1.subscribe("test", function (data, reply) { nc1.publish(reply, "ok"); });
nats2.subscribe("MOL.EVENTB.group.test.event", /*{ queue: "group" },*/ message => { broker.logger.info(kleur.magenta().bold("nodejs-subscriber2: Test event received with payload:"), message); });
nats.on('connect', () => { nats.subscribe("ping", (msg)=>{ console.log("Server1 got a ping:", msg); nats.publish("pong", "Hello from server1"); }); });
function setupNats() { const servers = Piloted.serviceHosts('nats'); if (!servers || !servers.length) { console.error('NATS not found'); return setTimeout(() => { setupNats(); }, 1000); } const natsServers = servers.map((server) => { return `nats://${process.env.NATS_USER}:${process.env.NATS_PASSWORD}@${server.address}:4222`; }); const nats = Nats.connect({ servers: natsServers }); nats.on('error', (err) => { console.log(err); }); // Subscribe for messages related to the sensor type subject // and create a queue group so that multiple instances don't // handle the same message nats.subscribe(internals.type, { queue: 'sensor' }, writeData); }
/** * Starts an internal nats queue * @method module:skyring/lib/timer#watch * @param {String} key The name of the nats queue to create * @param {Nodeback} callback Node style callback to execute when the function has finished execution **/ watch(key, cb) { if (this._bail) return const opts = {queue: key} this._sid = this.nats.subscribe(REBALANCE_SUB, opts, (data, reply) => { if (reply) this.nats.publish(reply, {node: this[kNode], timer: data.id}) if (this._bail) return cb(null, data) }) return this._sid }
/** * Starts an internal nats queue * @method module:skyring/lib/timer#watch * @param {String} key The name of the nats queue to create * @param {Nodeback} callback Node style callback to execute when the function has finished execution **/ watch(key, cb) { if (this._bail) return const opts = {queue: key} this._sid = this.nats.subscribe(REBALANCE_SUB, opts, (data, reply) => { if (reply) this.nats.publish(reply, {node: this[kNode], timer: data.id}) if (this._bail) return cb(null, data) }) return this._sid }