How to use the nats.connect function in nats

To help you get started, we’ve selected a few nats examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github hemerajs / hemera / test / hemera / types / index.ts View on Github external
import * as Hemera from './../../../packages/hemera'
import * as nats from 'nats'

type FileResult = {
  do: string
  isDone: boolean
}

const hemera = new Hemera(nats.connect('nats://127.0.0.1:4242'), {
  logLevel: 'debug'
})

hemera.ready(async (err: Error) => {
  // callback-style
  hemera.ext('onAct', function(hemera, next) {
    // some code
    next()
  })
  hemera.ext('onActFinished', function(hemera, next) {
    // some code
    next()
  })

  hemera.ext('preHandler', function(hemera, request, reply, next) {
    // some code
github nats-io / nats.ts / examples / node-sub.js View on Github external
#!/usr/bin/env node

/* jslint node: true */
'use strict';

var nats = require ('nats').connect();

nats.on('error', function(/*exception*/) {
  console.log('Can\'t connect to the nats-server [' + nats.options.url + '] is it running?');
});

var subject = process.argv[2];

if (!subject) {
  console.log('Usage: node-sub ');
  process.exit();
}

console.log('Listening on [' + subject + ']');

nats.subscribe(subject, function(msg) {
  console.log('Received "' + msg + '"');
github hemerajs / hemera / test / hemera / public-interface.spec.js View on Github external
it('public getters', function(done) {
    const nats = require('nats').connect(authUrl)

    const hemera = new Hemera(nats)

    expect(hemera.transport).to.be.exists()
    expect(hemera.topics).to.be.exists()
    expect(hemera.router).to.be.exists()

    hemera.close(done)
  })
})
github hemerajs / hemera / test / hemera / tracing-logs.spec.js View on Github external
it('Should log request and response logs', function(done) {
    const nats = require('nats').connect(authUrl)
    const stream = split(JSON.parse)
    const hemera = new Hemera(nats, {
      logLevel: 'debug',
      logger: stream
    })

    const logs = []

    stream.on('data', line => {
      logs.push(line)
    })

    hemera.ready(() => {
      hemera.add(
        {
          topic: 'math',
github nats-io / stan.js / test / basics.js View on Github external
stan.on('connect', () => {
            const connID = Buffer.from(stan.connId).toString('utf8');
            const nc = NATS.connect({
                encoding: "binary",
                preserveBuffers: true,
                port: PORT
            });
            nc.on('connect', () => {
                nc.subscribe(stan.pubPrefix + ".hello", (msg) => {
                    const pm = proto.pb.PubMsg.deserializeBinary(new Uint8Array(msg));
                    const pm_cid = pm.getClientId();
                    clientID.should.be.equal(pm_cid);
                    const pm_connid = Buffer.from(pm.getConnId()).toString('utf8');
                    connID.should.be.equal(pm_connid);
                    nc.close();
                    done();

                });
                nc.flush(() => {
github autopilotpattern / nodejs-example / smartthings / lib / index.js View on Github external
function setupNats(cb) {
  cb = cb || function () {};
  const servers = Piloted.serviceHosts('nats');

  if (!servers || !servers.length) {
    console.error('NATS not found');
    return setTimeout(() => { setupNats(cb); }, 1000);
  }

  const natsServers = servers.map((server) => {
    return `nats://${process.env.NATS_USER}:${process.env.NATS_PASSWORD}@${server.address}:4222`;
  });

  internals.nats = Nats.connect({ servers: natsServers });
  internals.nats.on('error', (err) => {
    console.log(err);
  });
  cb();
}
github moleculerjs / moleculer / dev / no-balancer.js View on Github external
transporter: "NATS",
	disableBalancer: true,
	logLevel: "info"
});

broker.start().then(() => {
	setInterval(() => {
		broker.logger.info(chalk.yellow("Send..."));
		//broker.emit("test.event", { ddata: "this is test data" }, ["group"]);
		broker.broadcast("test.event", { data: "this is test data" }, ["group"]);
		//broker.call("some.thing");
	}, 2000);
});


const nats1 = NATS.connect({
	url: "nats://localhost:4222",
	name: "nodejs-subscriber-1"
});

nats1.subscribe("MOL.EVENTB.group.test.event", /*{ queue: "group" },*/ message => {
	broker.logger.info(chalk.green.bold("nodejs-subscriber1: Test event received with payload:"), message);
});

const nats2 = NATS.connect({
	url: "nats://localhost:4222",
	name: "nodejs-subscriber-1"
});

nats2.subscribe("MOL.EVENTB.group.test.event", /*{ queue: "group" },*/ message => {
	broker.logger.info(chalk.magenta.bold("nodejs-subscriber2: Test event received with payload:"), message);
});
github hemerajs / hemera / examples / basic / onAdd-extension.js View on Github external
'use strict'

const Hemera = require('./../../packages/hemera')
const nats = require('nats').connect()

const hemera = new Hemera(nats, {
  logLevel: 'debug'
})

hemera.ready(() => {
  hemera.ext('onAdd', addDefinition => {
    hemera.log.info(addDefinition)
  })
  hemera.add(
    {
      topic: 'math',
      cmd: 'add'
    },
    (req, cb) => {
      cb(null, req.a + req.b)
github moleculerjs / moleculer / dev / no-balancer.js View on Github external
broker.broadcast("test.event", { data: "this is test data" }, ["group"]);
		//broker.call("some.thing");
	}, 2000);
});


const nats1 = NATS.connect({
	url: "nats://localhost:4222",
	name: "nodejs-subscriber-1"
});

nats1.subscribe("MOL.EVENTB.group.test.event", /*{ queue: "group" },*/ message => {
	broker.logger.info(chalk.green.bold("nodejs-subscriber1: Test event received with payload:"), message);
});

const nats2 = NATS.connect({
	url: "nats://localhost:4222",
	name: "nodejs-subscriber-1"
});

nats2.subscribe("MOL.EVENTB.group.test.event", /*{ queue: "group" },*/ message => {
	broker.logger.info(chalk.magenta.bold("nodejs-subscriber2: Test event received with payload:"), message);
});
github battila7 / brocan / components / services / hound / src / index.js View on Github external
const config = require('./config');

const logger = require('./logger').child({ component: 'main' });

const Hemera = require('nats-hemera');
const nats = require('nats').connect({
    url: config.get('nats.uri')
});

const Search = require('./search');

const search = Object.create(Search);
search.Search(config.get('elasticsearch.uri'), config.get('elasticsearch.index'));

const hemera = new Hemera(nats);

hemera.ready(function hemeraReady() {
    hemera.add({
        topic: 'build.query',
        entity: 'log',
    }, async function get(request) {
        logger.info('Getting logs for build "%s" from "%d" to "%d"', request.target, request.from, request.to);

nats

Node.js client for NATS, a lightweight, high-performance cloud native messaging system

Apache-2.0
Latest version published 4 months ago

Package Health Score

89 / 100
Full package analysis