buffered-messenger-node

0.1.0 • Public • Published

buffered-messenger-node

Build StatusCoverage StatusStandard - JavaScript Style Guide

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' })

Package Sidebar

Install

npm i buffered-messenger-node

Weekly Downloads

3,802

Version

0.1.0

License

Apache-2.0

Last publish

Collaborators

  • yunwang240