NSQ protocol for Node.js implemented as a through stream
NSQ protocol for Node.js implemented as a through stream
npm install nsq-protocol
var net = require('net');
var protocol = require('nsq-protocol');
var socket = net.connect(4150);
var p = protocol();
socket.pipe(p).pipe(socket);
p.identify({
short_id: 'maf',
long_id: 'mafintosh',
heartbeat_interval: 10000,
user_agent: 'my-client/0.1'
}, function() {
console.log('I identified!');
});
The following messages can be used
protocol.identify(options, [callback])
Send identify. See the spec for optionsprotocol.nop()
Send nop messageprotocol.sub(topic, channel, [callback])
Subscribe to a topic on a channelprotocol.pub(topic, data, [callback])
Publish to a topic. Data should be a buffer or stringprotocol.mpub(topic, list_of_data, [callback])
As above but with multiple messagesprotocol.rdy(count)
Indicate you are ready to receive messagesprotocol.fin(message_id)
Finish a message (when message processing succeeds)protocol.req(message_id, timeout)
Re-queue a message (when message processing fails)protocol.touch(message_id)
Reset the timeout for an in-flight messageprotocol.cls()
Send close messageprotocol.destroy()
Destroy the stream.The following events are emitted (excluding standard stream events)
protocol.on('message', message, message_id, attempts)
When a message is receivedprotocol.on('heartbeat')
When a heartbeat is receivedMIT