npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

amqp-messaging

v1.0.4

Published

connection manager and messaging packing and serialization for microservices using rabbitmq

Downloads

14

Readme

AMQP-Messaging

Highly efficient library to communicate with other microservices using RabbitMQ(AMQP Protocol) in nodejs

We're using GZIP and MsgPack for serialization. We also do packing to have a high throughput in Rabbitmq.

Installing

Install via NPM:

$ npm install --save amqp-messaging

Passing RabbitMQ Address:

you need to set rabbitmq host address in the ENV variable as below:

AMQP_SERVER_ADDRESS= 'rabbitmq'

Creating messaging variable

const Messaging = require('amqp-messaging');
const messaging = new Messaging();
//now you can work with messaging APIs

Work Queues

When you want to push to a queue and go ahead, You're not waiting for any responses. you can see this example:

await messaging.addWorker('mySampleQueue', async(data)=>{
  console.log(data.name);//Do whatever you want with data received
  await Promise.resolve();
}, {
  prefetchCount: 10
});
await messaging.sendPush('mySampleQueue', {
  name: 'Mehran'
});

default prefetchCount is set to 1. If you set it more than 1 Rabbitmq will give more than 1 items at once.

Publish/Subscribe

await messaging.subscribe('myQueueToSubscrie', async(data)=>{
  console.log(`message is received: ${data}`);//do what ever you want
  
});
await messaging.publish('myQueueToPublish', {
  sample: 'ok'
});

RPC

await messaging.addWorker('findUser', async(data)=>{
  return Promise.resolve({
     name: 'Mehran'
  });
}, {
  prefetchCount: 10,
  ttl: 1000
});

//In another microservice:
const user = await messaging.rpcCall('findUser', {
  id: '1234'
}, {
  ttl: 1000
});
console.log(`user is: ${user.name}`);

ttl in addWorker is the time to live for the queue in RabbitMQ, messages will be removed from the queue if ttl reached

ttl in rpcCall option is how much should the caller wait for the response? Promise will be rejected if TTL reached

It's prefered to set both ttl variables the same value.

Finish all jobs and stop all rabbitmq workers

You can stop Gracefully shutdown with command below. It will Pack and send All remaining items in the queue to Rabbitmq and cancels all Workers

const graceful = async () => {
  try {
    await messaging.cancelWorkers();
  } catch (e) {
    console.log('something bad happened', e);
  }
  await new Promise(resolve => setTimeout(resolve, 1000));
  process.exit(0);
};

process.on('SIGTERM', graceful);
process.on('SIGINT', graceful);
process.on('SIGUSR1', graceful);

##Packing This library is able to pack many messages into just one message and send a huge one to RabbitMQ Queue.

You need to set pack option in the sendPush method:

await messaging.addWorker('mySampleQueue', async(data)=>{
  console.log(data.name);//Do whatever you want with data received
  await Promise.resolve();
}, {
  prefetchCount: 10
});
await messaging.sendPush('mySampleQueue', {
  name: 'Mehran'
},{
  pack: {
    size: 3,
    interval: 3000//ms
  }
});
await messaging.sendPush('mySampleQueue', {
  name: 'Navid'
},{
  pack: {
    size: 3,
    interval: 3000//ms
  }
});

In this example, the library will not send messages Mehran and Navid immediately, it will wait until 3 seconds or 3 items to be packed together. Packing is very helpful to reduce Rabbitmq's high CPU usage by lowering publish rates.

Develop

We're open for pull requests. in order to run tests just run npm run test