npm/nsqjs

Name: nsqjs

Owner: npm

Description: NodeJS client library for NSQ

Forked from: dudleycarr/nsqjs

Created: 2017-04-25 20:52:40.0

Updated: 2018-01-22 18:48:31.0

Pushed: 2017-06-28 04:30:47.0

Homepage:

Size: 448

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

nsqjs

The official NodeJS client for the nsq client protocol. This implementation attempts to be fully compliant and maintain feature parity with the official Go (go-nsq) and Python (pynsq) clients.

Build Status

NPM

Usage
new Reader(topic, channel, options)

The topic and channel arguments are strings and must be specified. The options argument is optional. Below are the parameters that can be specified in the options object.

Reader events are:

Reader.MESSAGE and Reader.DISCARD both produce Message objects. Reader.NSQD_CONNECTED and Reader.NSQD_CLOSED events both provide the host and port of the nsqd to which the event pertains.

These methods are available on a Reader object:

Message

The following properties and methods are available on Message objects produced by a Reader instance.

new Writer(nsqdHost, nsqdPort, options)

Allows messages to be sent to an nsqd.

Available Writer options:

Writer events are:

These methods are available on a Writer object:

Simple example

Start nsqd and nsqdlookupd

qdLookupd Listens on 4161 for HTTP requests and 4160 for TCP requests
qlookupd &
qd --lookupd-tcp-address=127.0.0.1:4160 &
JavaScript
nsq = require('nsqjs');

reader = new nsq.Reader('sample_topic', 'test_channel', {
okupdHTTPAddresses: '127.0.0.1:4161'


er.connect();

er.on('message', function (msg) {
nsole.log('Received message [%s]: %s', msg.id, msg.body.toString());
g.finish();

CoffeeScript
= require 'nsqjs'

c = 'sample_topic'
nel = 'test_channel'
ons =
okupdHTTPAddresses: '127.0.0.1:4161'

er = new nsq.Reader topic, channel, options
er.connect()

er.on nsq.Reader.MESSAGE, (msg) ->
nsole.log "Received message [#{msg.id}]: #{msg.body.toString()}"
g.finish()

Publish a message to nsqd to be consumed by the sample client:

rl -d "it really tied the room together" http://localhost:4151/pub?topic=sample_topic
Example with message timeouts

This script simulates a message that takes a long time to process or at least longer than the default message timeout. To ensure that the message doesn't timeout while being processed, touch events are sent to keep it alive.

JavaScript
nsq = require('nsqjs');

reader = new nsq.Reader('sample_topic', 'test_channel', {
okupdHTTPAddresses: '127.0.0.1:4161'


er.connect();

er.on('message', function (msg) {
nsole.log('Received message [%s]', msg.id);

nction touch() {
if (!msg.hasResponded) {
  console.log('Touch [%s]', msg.id);
  msg.touch();
  // Touch the message again a second before the next timeout.
  setTimeout(touch, msg.timeUntilTimeout() - 1000);
}


nction finish() {
console.log('Finished message [%s]: %s', msg.id, msg.body.toString());
msg.finish();


nsole.log('Message timeout is %f secs.', msg.timeUntilTimeout() / 1000);
tTimeout(touch, msg.timeUntilTimeout() - 1000);

 Finish the message after 2 timeout periods and 1 second.
tTimeout(finish, msg.timeUntilTimeout() * 2 + 1000);

CoffeeScript
der} = require 'nsqjs'

c = 'sample_topic'
nel = 'test_channel'
ons =
okupdHTTPAddresses: '127.0.0.1:4161'

er = new Reader topic, channel, options
er.connect()

er.on Reader.MESSAGE, (msg) ->
nsole.log "Received message [#{msg.id}]"

uch = ->
unless msg.hasResponded
  console.log "Touch [#{msg.id}]"
  msg.touch()
  # Touch the message again a second before the next timeout.
  setTimeout touch, msg.timeUntilTimeout() - 1000

nish = ->
console.log "Finished message [#{msg.id}]: #{msg.body.toString()}"
msg.finish()

nsole.log "Message timeout is #{msg.timeUntilTimeout() / 1000} secs."
tTimeout touch, msg.timeUntilTimeout() - 1000

Finish the message after 2 timeout periods and 1 second.
tTimeout finish, msg.timeUntilTimeout() * 2 + 1000
Enable nsqjs debugging

nsqjs uses debug to log debug output.

To see all nsqjs events:

BUG=nsqjs:* node my_nsqjs_script.js

To see all reader events:

BUG=nsqjs:reader:* node my_nsqjs_script.js

To see a specific reader's events:

BUG=nsqjs:reader:<topic>/<channel>:* node my_nsqjs_script.js

Replace <topic> and <channel>

To see all writer events:

BUG=nsqjs:writer:* node my_nsqjs_script.js
A Writer Example

The writer sends a single message and then a list of messages.

JavaScript
nsq = require('nsqjs');

w = new nsq.Writer('127.0.0.1', 4150);

nnect();

('ready', function () {
publish('sample_topic', 'it really tied the room together');
publish('sample_topic', [
'Uh, excuse me. Mark it zero. Next frame.', 
'Smokey, this is not \'Nam. This is bowling. There are rules.'
;
publish('sample_topic', 'Wu?', function (err) {
if (err) { return console.error(err.message); }
console.log('Message sent successfully');
w.close();
;


('closed', function () {
nsole.log('Writer closed');

CoffeeScript
ter} = require 'nsqjs'

new Writer '127.0.0.1', 4150
nnect()

 Writer.READY, ->
publish 'sample_topic', 'it really tied the room together'
publish 'sample_topic', ['Uh, excuse me. Mark it zero. Next frame.', 
'Smokey, this is not \'Nam. This is bowling. There are rules.']
publish 'sample_topic', 'Wu?', (err) ->
console.log 'Message sent successfully' unless err?
w.close()

 Writer.CLOSED, ->
nsole.log 'Writer closed'
Changes

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.