buffered-messenger-node
v0.1.0
Published
Buffered messenger for Node.js - Accumulate the messages in the buffer, and flushe/send for certain interval
Downloads
11,154
Readme
buffered-messenger-node
Buffers the messages in certain interval as the buffer size allowed; and send through the function either user-implemented or provided.
Install
Install with
npm install buffered-messenger-node --save
Usage
To get started, initialize a new instance with a messageHandler, which will be used to flush the messages after certain interval or when the buffer is full.
const Messenger = require('buffered-messenger-node')
const messenger = new Messenger({
handler: (message) => {
messages.forEach((message) => {
console.log(JSON.stringify(message))
}
}
})
Or to use the provided messageHandler
const rp = require('request-promise') // or any other library needed to send the messages
const Messenger = require('buffered-messenger-node')
const messenger = new Messenger({
maxBufferSize: 5, // defaults to 10 items in the buffer array
flushInterval: 1000, // milliseconds - defaults to 10000 or 10 secs
handler: (messages) => {
// return promise from request-promise
return rp({
method: 'POST',
uri: 'http://api.posttestserver.com/post',
body: JSON.stringify(messages)
})
}
})
To pass a message to the buffered messenger
client.send('sample message')
Or pass a message object
client.send({ message: 'test-message', trace: 'my-trace' })