NATS - Node.js Client
A Node.js client for the NATS messaging system.
Installation
npm install nats
Basic Usage
var nats = require('nats').connect();
nats.publish('foo', 'Hello World!');
nats.subscribe('foo', function(msg) {
console.log('Received a message: ' + msg);
});
var sid = nats.subscribe('foo', function(msg) {});
nats.unsubscribe(sid);
var sid = nats.request('request', function(response) {
console.log('Got a response in msg stream: ' + response);
});
nats.request('help', null, {'max':1}, function(response) {
console.log('Got a response for help: ' + response);
});
nats.subscribe('help', function(request, replyTo) {
nats.publish(replyTo, 'I can help!');
});
nats.close();
Wildcard Subscriptions
nats.subscribe('foo.*.baz', function(msg, reply, subject) {
console.log('Msg received on [' + subject + '] : ' + msg);
});
nats.subscribe('foo.bar.*', function(msg, reply, subject) {
console.log('Msg received on [' + subject + '] : ' + msg);
});
nats.subscribe('foo.>', function(msg, reply, subject) {
console.log('Msg received on [' + subject + '] : ' + msg);
});
Queue Groups
nats.subscribe('foo', {'queue':'job.workers'}, function() {
received += 1;
});
Clustered Usage
var nats = require('nats');
var servers = ['nats://nats.io:4222', 'nats://nats.io:5222', 'nats://nats.io:6222'];
var nc = nats.connect({'servers': servers});
console.log("Connected to " + nc.currentServer.host);
nc = nats.connect({'dontRandomize': true, 'servers':servers});
TLS
var nats = require('nats');
var fs = require('fs');
var nc = nats.connect({port: TLSPORT, tls: true});
var tlsOptions = {
rejectUnauthorized: false,
};
var nc = nats.connect({port: TLSPORT, tls: tlsOptions});
var tlsOptions = {
ca: [ fs.readFileSync('./test/certs/ca.pem') ]
};
var nc = nats.connect({port: TLSPORT, tls: tlsOptions});
var tlsOptions = {
key: fs.readFileSync('./test/certs/client-key.pem'),
cert: fs.readFileSync('./test/certs/client-cert.pem'),
ca: [ fs.readFileSync('./test/certs/ca.pem') ]
};
var nc = nats.connect({port: TLSPORT, tls: tlsOptions});
Advanced Usage
nats.publish('foo', 'You done?', function() {
console.log('msg processed!');
});
nats.flush(function() {
console.log('All clear!');
});
var nc = nats.connect({port: PORT, yieldTime: 10});
var sid = nats.subscribe('foo', function() {
received += 1;
});
nats.timeout(sid, timeout_ms, expected, function() {
timeout = true;
});
nats.subscribe('foo', {'max':MAX_WANTED});
nats.unsubscribe(sid, MAX_WANTED);
var nats = require('nats');
var nc1 = nats.connect();
var nc2 = nats.connect();
nc1.subscribe('foo');
nc2.publish('foo');
nc = nats.connect({'servers':servers, 'encoding': 'ascii'});
See examples and benchmarks for more information..
License
(The MIT License)
Copyright (c) 2015 Apcera Inc.
Copyright (c) 2011-2015 Derek Collison
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.