1.0.1 • Published 7 years ago

nats-event-ex v1.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

Nats Event EX

Extends nats event with enforced protocol.

Features

  • Protocol enforced
  • Default error handler

Protocol

protocol

Usage

Install package:

npm install nats-event-ex

Import an use:

const eventEx = require('nats-event-ex')('nats://localhost:4222')
  .setQueueGroup(SERVICE_NAME)

Basic Usage:

eventEx.listen('book.created', async (book) => {await readBook(book)})

eventEx.send('book.created', {name: 'Good Book'})

Default error handler:

eventEx.listen('error', async () => {throw new Error('Wops!')})

eventEx.send('error')
// error will be logged by simple-json-error

API

This module is based on nats-event, and that module is based on node-nats. Please check their docs for more detailed apis.

module.exports

func(options) => eventEx

  • options could be:

    • an nats url string for single server, such as nats://localhost:4222
    • an nats url string for cluster servers, such as nats://192.168.0.1:4222,nats://192.168.0.2:4222
    • nats connect options

eventEx.listen

func(name, handler)

  • handler: async (data, input, subject)

eventEx.send

async func(name, data, options)

  • options is optional, which may contains:
    • eventId: optional