How to use nats - 10 common examples

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 nats-io / stan.js / lib / stan.js View on Github external
Stan.prototype.subscribe = function(subject, qGroup, options) {
    const args = {};
    if (typeof qGroup === 'string') {
        args.qGroup = qGroup;
    } else if (typeof qGroup === 'object') {
        args.options = qGroup;
    }
    if (typeof options === 'object') {
        args.options = options;
    }
    if (!args.options) {
        args.options = new SubscriptionOptions();
    }

    // in node-nats there's no Subscription object...
    const retVal = new Subscription(this, subject, args.qGroup, nats.createInbox(), args.options, args.callback);

    if (typeof subject !== 'string' || subject.length === 0) {
        process.nextTick(() => {
            retVal.emit('error', new Error(BAD_SUBJECT));
        });
        return retVal;
    }

    if (this.isClosed()) {
        process.nextTick(() => {
            retVal.emit('error', new Error(CONN_CLOSED));
        });
        return retVal;
    }

    this.subMap[retVal.inbox] = retVal;
github fanyang01 / crawler / electron / client / main.js View on Github external
const util = require('util');
const fs = require('fs');
const Websocket = require('ws');
const NATS = require('nats');
const electron = require('electron');
const app = electron.app; // Module to control application life.
const ipcMain = electron.ipcMain;
const BrowserWindow = electron.BrowserWindow; // Module to create native browser window.


function Emitter() {
  EventEmitter.call(this);
}
util.inherits(Emitter, EventEmitter);

const natsURL = process.env.NATS_URL || NATS.DEFAULT_URI;
const websocketURL = process.env.WEBSOCKET_URL || 'ws://localhost:8162';
const connMode = process.env.CONN_MODE || '';

var ws = null;
var nats = null;
var clientID = null;
// windows = [],
// working = [];

// Quit when all windows are closed.
app.on('window-all-closed', function() {
  // if (process.platform != 'darwin') {
  //   app.quit();
  // }
});
github hemerajs / hemera / packages / hemera / lib / index.js View on Github external
_sendRequestHandler(response) {
    const self = this

    if (response.code && response.code === NATS.REQ_TIMEOUT) {
      self._timeoutHandler()
      return
    }

    const res = self._clientDecoder(response)
    self.response.payload = res.value
    self.response.error = res.error

    // decoding error
    if (self.response.error) {
      const internalError = new Errors.ParseError('Client payload decoding', self.errorDetails).causedBy(
        self.response.error
      )
      self.log.error(internalError)
      self._execute(self.response.error)
      return
github nats-io / stan.js / lib / stan.js View on Github external
this.nc.on('connect', () => {
        // heartbeat processing
        const hbInbox = nats.createInbox();
        this.hbSubscription = this.nc.subscribe(hbInbox, (msg, reply) => {
            this.nc.publish(reply);
        });

        this.pingInbox = nats.createInbox();
        this.pingSubscription = this.nc.subscribe(this.pingInbox, (msg) => {
            if (msg) {
                const pingResponse = proto.pb.PingResponse.deserializeBinary(Buffer.from(msg, 'binary'));
                const err = pingResponse.getError();
                if (err) {
                    this.closeWithError('connection_lost', err);
                    return;
                }
            }
            this.pingOut = 0;
        });

        this.ackSubscription = this.nc.subscribe(this.ackSubject, this.processAck());

        const discoverSubject = this.options.discoverPrefix + '.' + this.clusterID;
        //noinspection JSUnresolvedFunction
github nats-io / stan.js / lib / stan.js View on Github external
this.nc.on('connect', () => {
        // heartbeat processing
        const hbInbox = nats.createInbox();
        this.hbSubscription = this.nc.subscribe(hbInbox, (msg, reply) => {
            this.nc.publish(reply);
        });

        this.pingInbox = nats.createInbox();
        this.pingSubscription = this.nc.subscribe(this.pingInbox, (msg) => {
            if (msg) {
                const pingResponse = proto.pb.PingResponse.deserializeBinary(Buffer.from(msg, 'binary'));
                const err = pingResponse.getError();
                if (err) {
                    this.closeWithError('connection_lost', err);
                    return;
                }
            }
            this.pingOut = 0;
        });
github hemerajs / hemera / packages / hemera / lib / constants.js View on Github external
*
 * This source code is licensed under the MIT-style license found in the
 * LICENSE file in the root directory of this source tree.
 *
 */
const NATS = require('nats')

module.exports = {
  // General
  NATS_QUEUEGROUP_PREFIX: 'queue',
  // NATS conn error codes
  NATS_CONN_ERROR_CODES: [
    NATS.CONN_ERR,
    NATS.SECURE_CONN_REQ,
    NATS.NON_SECURE_CONN_REQ,
    NATS.CLIENT_CERT_REQ
  ],
  // NATS errors
  NATS_TRANSPORT_ERROR: 'Could not connect to NATS!',
  NATS_TRANSPORT_CLOSED: 'NATS connection closed!',
  NATS_TRANSPORT_CONNECTED: 'Connected!',
  NATS_PERMISSION_ERROR: 'NATS permission error',
  NATS_TRANSPORT_RECONNECTING: 'NATS reconnecting ...',
  NATS_TRANSPORT_RECONNECTED: 'NATS reconnected!',
  NATS_TRANSPORT_DISCONNECTED: 'NATS disconnected!',
  // Hemera request types
  REQUEST_TYPE_PUBSUB: 'pubsub',
  REQUEST_TYPE_REQUEST: 'request',
  // Application errors
  TOPIC_SID_REQUIRED_FOR_DELETION: 'Topic or sid is required for deletion',
  ACT_TIMEOUT_ERROR: 'Timeout',
  NO_TOPIC_TO_SUBSCRIBE: 'No topic to subscribe',
github hemerajs / hemera / packages / hemera / lib / constants.js View on Github external
/**
 * Copyright 2016-present, Dustin Deus (deusdustin@gmail.com)
 * All rights reserved.
 *
 * This source code is licensed under the MIT-style license found in the
 * LICENSE file in the root directory of this source tree.
 *
 */
const NATS = require('nats')

module.exports = {
  // General
  NATS_QUEUEGROUP_PREFIX: 'queue',
  // NATS conn error codes
  NATS_CONN_ERROR_CODES: [
    NATS.CONN_ERR,
    NATS.SECURE_CONN_REQ,
    NATS.NON_SECURE_CONN_REQ,
    NATS.CLIENT_CERT_REQ
  ],
  // NATS errors
  NATS_TRANSPORT_ERROR: 'Could not connect to NATS!',
  NATS_TRANSPORT_CLOSED: 'NATS connection closed!',
  NATS_TRANSPORT_CONNECTED: 'Connected!',
  NATS_PERMISSION_ERROR: 'NATS permission error',
  NATS_TRANSPORT_RECONNECTING: 'NATS reconnecting ...',
  NATS_TRANSPORT_RECONNECTED: 'NATS reconnected!',
  NATS_TRANSPORT_DISCONNECTED: 'NATS disconnected!',
  // Hemera request types
  REQUEST_TYPE_PUBSUB: 'pubsub',
  REQUEST_TYPE_REQUEST: 'request',
  // Application errors
github hemerajs / hemera / packages / hemera / lib / constants.js View on Github external
* All rights reserved.
 *
 * This source code is licensed under the MIT-style license found in the
 * LICENSE file in the root directory of this source tree.
 *
 */
const NATS = require('nats')

module.exports = {
  // General
  NATS_QUEUEGROUP_PREFIX: 'queue',
  // NATS conn error codes
  NATS_CONN_ERROR_CODES: [
    NATS.CONN_ERR,
    NATS.SECURE_CONN_REQ,
    NATS.NON_SECURE_CONN_REQ,
    NATS.CLIENT_CERT_REQ
  ],
  // NATS errors
  NATS_TRANSPORT_ERROR: 'Could not connect to NATS!',
  NATS_TRANSPORT_CLOSED: 'NATS connection closed!',
  NATS_TRANSPORT_CONNECTED: 'Connected!',
  NATS_PERMISSION_ERROR: 'NATS permission error',
  NATS_TRANSPORT_RECONNECTING: 'NATS reconnecting ...',
  NATS_TRANSPORT_RECONNECTED: 'NATS reconnected!',
  NATS_TRANSPORT_DISCONNECTED: 'NATS disconnected!',
  // Hemera request types
  REQUEST_TYPE_PUBSUB: 'pubsub',
  REQUEST_TYPE_REQUEST: 'request',
  // Application errors
  TOPIC_SID_REQUIRED_FOR_DELETION: 'Topic or sid is required for deletion',
  ACT_TIMEOUT_ERROR: 'Timeout',

nats

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

Apache-2.0
Latest version published 5 months ago

Package Health Score

89 / 100
Full package analysis