Git Product home page Git Product logo

amqp-bus's Introduction

amqp-bus

A bus-like facade over AMQP for simple messaging patterns. Makes using RabbitMQ for idomatic messaging dead simple.

The interface and routing topology are opinionated, and based loosely on ideas from the MassTransit project and various best-practice articles.

Introduction

The library provides a Bus class, which as has seven functions:

  • start
  • stop
  • publish
  • subscribe
  • unsubscribe
  • call
  • bind

Bus is also an EventEmitter, and raises 'started' and 'stopped' events, as well as log info events: 'debug', 'info', and 'error'.

Basic Usage

Bus

A bus object manages the connections, channels, reconnection/disconnection logic. The creating and deleting of exchanges and queues is also handled automatically.

const Bus = require('amqp-bus');

// Create a bus instance. 
// By default connection will be to guest:guest@localhost.
// See docs for options (host, vhost, credentials, etc.)
const bus = new Bus();

// start the bus.  This creates the connection, among other things.
bus.start();

// When bus is started, you can use it.
bus.on('started', ()=> {

  //do stuff with the bus...
  
});

Depending on the role of the application, you can do various things with the bus.

As a subscriber:

Subscribe to a named message type that could be published, such as 'user.created', supplying a handler function to process the received messages.

const subscriptionOptions = {
  messageType: 'user.created'
};
bus.subscribe(subscriptionOptions, (message) => {
  // message is the JSON object that was published.
  // do something with message...
});

As a message type publisher:

Broadcast a message to any/all subscribers.

const userCreatedEvent = {
  id: 42
  name 'john'
};

const publishOptions = {
  messageType: 'user.created'
};

bus.publish(publishOptions, userCreatedEvent);

As a topic publisher:

Publish using a known exchange and routing key, to route message to specific consumer(s).

const publishOptions = {
  exchangeName: 'user.notifications',
  routingKey: 'notifications.user.42'
};

const message = {
  notification : 'something cool happend'
};

bus.publish(publishOptions, message);

As a topic subscriber:

Subscribe to messages from a known exchange with a specific routing key, or key pattern.

const subscriptionOptions = {
  exchangeName: 'user.notifications',
  routingKey: 'notifications.user.42'
};

const handler = (message) => {
  // do something with received messages.
};

let subscription;
bus.subscribe(subscriptionOptions, handler)
  .then((result) => {
    subscription = result
  });

//later, you can unsubscribe to stop receiving messages and clean up.
bus.unsubscribe(subscription);

Note: the unsubscribe can also be used with the messageType subscriptions, but may not be as useful.

As an RPC callee:

Bind a handler function to a message type. The return value of the handler will be sent back to caller as the reply.

const handleRequest = (message) => {
  return {
    id: 42,
    name: 'john'
  };
  // You may also return a promise, and the resolved value will be sent as the reply.
};

bus.bind('user.query.by-id', handler);

As an RPC caller:

Call with a message type and receive the reply in a promise.

const message = {
  id: 42
};
bus.call('user.query.by-id', message)
  .then((reply) => {
     // do something with the reply...
  });
    

Stopping the bus:

// later, to clean up and disconnect:
bus.stop();

API docs

coming soon

amqp-bus's People

Contributors

davidwmartines avatar d5172 avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar  avatar

amqp-bus's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.