Coder Social home page Coder Social logo

amqp-rabbit's Introduction

amqp-rabbit

Simple RabbitMQ library that built on top of amqplib

INSTALLATION

npm install --save amqp-rabbit

FEATURES

  • send/listen
  • request/reply
  • publish/subscribe
  • auto reconnect

Setting up a connection

  const Rabbit = require('amqp-rabbit')
  const rabbit = new Rabbit('amqp://localhost:5672')

Usage


request-reply

Process B makes an rpc request to 'add' queue and received by Process A 'add' queue handler. The handler sends a reply by returning an object.

NOTE: message to send and return value must be an object

Process A:

  // receive a request
  rabbit.reply('add', (message) => {
    return {
      result: message.num1 + message.num2
    }
  })

Process B:

  // send a request
  rabbit.request('add', { num1: 10, num2: 15 })
    .then(rep => {
      console.log(rep.result) // 25
    })

Alternative Process B:

  
  const add = rabbit.setupRequest('add');
  // send a request
  add({ num1: 10, num2: 15 })
    .then(rep => {
      console.log(rep.result) // 25
    })

send - listen

Make a simple sending and receiving of messages in a 1:1 sender:listener fashion. Process B send a message every 1 second to Process A.

Process A:

  
  // listen to an event
  rabbit.listen('event', (message) => {
    console.log(message) // { msg: 'hello from Process B'}
  })

Process B:

  setInterval(() => {
    rabbit.send('event', { msg: 'hello from Process B'})
  }, 1000)

publish - subscribe

Make a simple sending and receiving of messages in a 1:N fashion. Looks similar to send - listen, the difference is one sends a message and any subsribers can receive.

Process A:

  rabbit.subscribe('broadcast', (message) => {
    console.log(message) // { msg: 'this is a broadcast message'}
  })

Process B:

  rabbit.subscribe('broadcast', (message) => {
    console.log(message) // { msg: 'this is a broadcast message'}
  })

Process C:

  setInterval(() => {
    rabbit.publish('broadcast', { msg: 'this is a broadcast message'})
  }, 1000)

amqp-rabbit's People

Contributors

coyksdev avatar

Watchers

James Cloos avatar  avatar

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.