开发者社区> 镜像站> NPM> mq-pubsub
mq-pubsub
Pub/sub pattern for microservices using RabbitMQ.
Last updated 5 years ago by philcockfield .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install mq-pubsub 
SYNC missed versions from official npm registry.

mq-pubsub

Build Status

Pub/sub pattern for microservices using RabbitMQ.

See also:

Installation

npm install --save mq-pubsub

Usage

Initialize a connection to a RabbitMQ server:

import pubsubFactory from "mq-pubsub";
const pubsub = pubsubFactory("amqp://rabbitmq");

Create individual event-managers by passing the event's name to the event method:

const myEvent = pubsub.event("my-event");

To listen for events, pass a handler to the subscribe method:

myEvent.subscribe(data => {
  console.log("event:", data);
});

To publish events, push data to the publish method:

myEvent.publish({ foo: 123 });

These methods can be used immediately without waiting for the connection to complete. The module manages the connection state internally, and buffers events until the RabbitMQ connection is ready.

Example

Look in the /example folder, you will need to change the "amqp://" URL in the pub.js and sub.js files to your RabbitMQ server.

By default the examples assume a RabbitMQ server running within docker (on 192.168.99.100), which you can start with:

npm run example:up
  • Subscribe: Start one or more consoles as subscribed listeners to the "foo" and "bar" events:

    npm run example:sub
    
  • Publish: In another console run the publish script:

    npm run example:pub <event> <total>
    

    For example:

    npm run example:pub           # Fires the "foo" event one time.
    npm run example:pub foo 15    # Fires the "foo" event 15 times.
    npm run example:pub bar 101   # Fires the "bar" event 101 times.
    

Tests

npm test

Docker Commands

docker run -it -p 5672:5672 rabbitmq

License: MIT

Current Tags

  • 1.1.2                                ...           latest (5 years ago)

7 Versions

  • 1.1.2                                ...           5 years ago
  • 1.1.1                                ...           5 years ago
  • 1.1.0                                ...           5 years ago
  • 1.0.2                                ...           5 years ago
  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
  • 0.0.1                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (4)
Dependents (1)