spawnpoint-nats v2.2.0
spawnpoint-nats.js
NATS Plugin for Spawnpoint on NPM
Installation
Make sure to install NATS and the plugin for Spawnpoint NATS separately. NATS is treated as a peer dependency so you can change version separately.
npm i nats spawnpoint-nats
NATS Versions
- This module's version
2.x.xis designed for a NATS server running2.x - This module's version
1.x.xis designed for a NATS server running1.x
API
This api is mounted at app.nats to access these methods:
app.nats.publish(subject, message, callback)
Sends a message to a subscriber. Expects no response. Callback issued when message is sent to the NATS server. No guarantee of receipt.
subjectstring - subject to publish message tomessageobject|array|string|buffer - Message body for published messagecallbackfunction - Optional callback fired when sent
app.nats.request(subject, message, options, callback, updateCallback)
Sends a message to a subscriber with the expectation of at least one reply. Main callback only listens for the final reply. Can get updates to provide realtime stats/progress and/or acknowledgments to reset timeouts. Returns event emitter for update, ack, response, & timeout events.
subjectstring - subject to publish message tomessageobject|array|string|buffer - Message body for published messageoptionsobject - Options passed toPUBmethod.maxnumber - Number of replies to receive before unsubscribing from replies, optionaltimeoutnumber - Number of ms to wait until firing a timeout error. If omitted it will default to config settings. Setting to falsey value will disable timeout
callbackfunction - Optional callback fired when final response is sent. Callback includes two argumentserrerror|null - If the response failed via timeout or was reported as an error from the response.results*object|array|string|buffer - Response body
updateCallbackfunction - Optional callback fired when update is sent. Callback includes one argumentresultsobject|array|string|buffer - Response body
Example:
app.nats.request('lookup', {
domain: "google.com"
}, function(err, results){
if(err){
return console.error('ERROR', err);
}
console.log('Lookup results', results);
}, function(update){
console.log('update', update);
});app.nats.subscribe(subject, options, callback, updateCallback)
Sends a message to a subscriber with the expectation of at least one reply. Main callback only listens for the final reply. Can get updates to provide realtime stats/progress and/or acknowledgments to reset timeouts. Returns event emitter for update, ack, response, & timeout events.
subjectstring - subject to subscribe tooptionsobject - Options passed toPUBmethod.queuestring - Name of queue to joinmaxnumber - Maximum number of messages to receive before automatically unsubscribing.noAckboolean - Prevents automaticackmessage when set to true. Defaults to false.noPrefixstring - Prevents configurable prefix string from adding to subject. Defaults to false.
callbackfunction - callback fired when messages are received. Callback includes two argumentsresponseobject|array|string|buffer - Message bodyhandlereventEmitter - Event emitter with helper methods to handle updates, acks, and replies.- Emittable EVENTS - These events need to be emitted to send messages
ack- timeout int: Resets the timeout for the request. If not timeout is specified it will reset the timer that was used by the requester.timeoutis in ms.update- data string: Sends a message with data update the request. Also resets the timeout.response- err error|null, results object|array|string|buffer: Sends an error or response to the request
- Helper Methods - These methods are helper methods to make emitting the events above easier
ack(): Tells the request to reset timeout, has no body or data to sendupdate(data *string*): Sends a message with data update the request. Also resets the timeout.response(err *error|null*, results *object|array|string|buffer*): Sends an error or response to the request
- Emittable EVENTS - These events need to be emitted to send messages
subjectstring - Copy of the message subject. Useful for wildcard subscriptions. Does not include prefix where applicable.
Example:
app.nats.handle('lookup', {
queue: "dns.lookup"
}, function(msg, handler){
setTimeout(function(){
handler.ack();
}, 2500);
setTimeout(function(){
handler.update({
pending: true
});
}, 5000);
setTimeout(function(){
handler.response(null, msg);
}, 7500);
});2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago