hapi-nsqjs

Hapi.js plugin to support nsq

Usage no npm install needed!

<script type="module">
  import hapiNsqjs from 'https://cdn.skypack.dev/hapi-nsqjs';
</script>

README

hapi-nsq

Hapi-NSQ Hapi.js module for using NSQ

Lead Maintainer: Doron Segal

Introduction

If you're using Hapi.js and want to use NSQ this plugin can help you.

Example

Initializing the plugin (NSQD)

server = new Hapi.Server();
server.connection();
server.register([{
  register: require('hapi-nsqjs'),
  options: {
    nsqPort: 4150,
    nsqHost: '127.0.0.1'
  }
}], (err) => {
  if (err) {
    throw err;
  }
  //server is running
});

Initializing the plugin (NSQ LOOKUP)

server = new Hapi.Server();
server.connection();
server.register([{
  register: require('hapi-nsqjs'),
  options: {
    nsqLookup: ['10.0.0.1:9988','10.0.0.2:9988']
  }
}], (err) => {
  if (err) {
    throw err;
  }
  //server is running
});

Publish

const TOPIC = 'events'; //Your topic
//Access plugin via server object
server.plugins['hapi-nsqjs'].writer.publish(TOPIC, ['foo', 'bar', 'baz']);
server.plugins['hapi-nsqjs'].writer.publish('events', 'some message here');

// Access the plugin via request
request.server.plugins['hapi-nsqjs'].writer.publish(TOPIC, ['foo', 'bar', 'baz']);
request.server.plugins['hapi-nsqjs'].writer.publish('events', 'some message here');

Subscribe using the module

// using nsq.js
exports.register = function (server, options, next) {
  const reader = server.plugins['hapi-nsqjs'].nsq.reader({
    nsqlookupd: Config.get('/nsq/lookupd'), // you can use the `nsqd: ['0.0.0.0:4150'],`
    maxInFlight: Config.get('/nsq/maxInFlight'),
    topic: Config.get('/nsq/topic'),
    channel: Config.get('/nsq/channel')
  });

  reader.on('message', (msg) => {
    console.log(msg.toString());
    setTimeout(() => {
      msg.finish();
    }, 200);
  });

  reader.on('ready', () => {
    console.log('nsq is ready');
    next();
  });
});

Subscribe Without using the module

// using nsq.js
var nsq = require('nsq.js');
var reader = nsq.reader({
  nsqd: ['0.0.0.0:4150'],
  maxInFlight: 5,
  topic: 'events',
  channel: 'ingestion'
});

reader.on('message', function(msg){
  console.log(msg.body.toString());
  setTimeout(function(){
    msg.finish();
  }, 200);
});

Plugin Options:

  • nsqLookup
    • When using nsqlookupd you need to pass an array with ip:port
    • for ex' nsqLookup: ['127.0.0.1:5000', '128.0.0.1:5000']
  • nsqd
    • When using nsqd you need to pass host and port
    • for ex' javascript { nsqPort: 5000, nsqHost: '127.0.0.2' }

Logs

Logs will be using server.log['nsq]

NSQ Important links

  • for more example using nsq.js checkout this url
  • Great article by Calvin French-Owen from Segment url

API

Coming Soon