Skip to content

librpc/rmq-rpc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RMQ-RPC

RabbitMQ RPC client and server

js-standard-style

Table of Contents

Features

  • Simple
  • Lightweight
  • Promise based
  • Easiest API as possible
  • Designed with performance in mind

Install

Usage

// server.js
var open = require('amqplib').connect('amqp://localhost')
var RpcServer = require('rmq-rpc').Server

var server = new RpcServer({
  name: 'math',
  methods: {
    add ({ x, y }) {
      console.log('add', x, y)
      return Number(x) + Number(y)
    },

    sub ({ x, y }) {
      console.log('sub', x, y)
      return Number(x) - Number(y)
    },

    mul ({ x, y }) {
      console.log('mul', x, y)
      return Number(x) * Number(y)
    },

    div ({ x, y }) {
      console.log('div', x, y)
      return Number(x) / Number(y)
    }
  }
})

function main (channel) {
  server.setup(channel)
}

open
  .then(connection => connection.createChannel())
  .then(main)
  .catch(console.warn)
// client.js
var express = require('express')
var argv = require('minimist')(process.argv.slice(2))
var open = require('amqplib').connect('amqp://localhost')
var RpcClient = require('rmq-rpc').Client

var client = new RpcClient({ name: 'math' })

function main (channel) {
  client.setup(channel)
}

open
  .then(connection => connection.createChannel())
  .then(main)
  .catch(console.warn)

var app = express()

app.get('/add/:x/:y', function (req, res) {
  client.call('add', req.params)
    .then(result => res.send(String(result)))
})

app.get('/sub/:x/:y', function (req, res) {
  client.call('sub', req.params)
    .then(result => res.send(String(result)))
})

app.get('/mul/:x/:y', function (req, res) {
  client.call('mul', req.params)
    .then(result => res.send(String(result)))
})

app.get('/div/:x/:y', function (req, res) {
  client.call('div', req.params)
    .then(result => res.send(String(result)))
})

app.listen(argv.port, function () {
  console.log(`Listening on port ${argv.port}`)
})

API

RpcServer

#constructor({ name, methods })

#setup(channel)

#emit(eventName, data)

RpcClient

#constructor({ name })

#setup(channel)

#call(method, data, { timeout = 5000 })

#on(eventName, handler)

#off(eventName, handler)

Development

Command Description
npm run check Check standard code style by snazzy

About

RabbitMQ RPC client and server

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published