trusty-rabbit
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

Trusty Rabbit (Alpha Version)

Trustworthy and easy to use RabbitMQ client, supports auto-reconnect and graceful stop.

Quick Start

Installation

$ npm i trusty-rabbit

Publish Messages

import {
  Connection,
  Publisher,
} from 'trusty-rabbit';
 
const connection = new Connection({
  uri: 'amqp://user:password@127.0.0.1:5672/my_vhost',
  // Try to reconnect every 1 second. 
  reconnect: async () => {
    // You have to implement `sleep` function by yourself. 
    await sleep(1000);
 
    return true;
  },
});
const publisher = new Publisher(connection, {
  exchange: {
    name: exchangeName,
  },
});
 
// Passing messageID, routing key and content (content must be JSON-stringifiable), done!
await publisher.publish('messageID', 'key.0', {
  value: 'content',
});
 
setTimeout(async () => {
  // Close publisher and underlying connection by passing `true`.
  await publisher.close(true);
}, 2000);

Consume Messages

import {
  Connection,
  Consumer,
} from 'trusty-rabbit';
 
const connection = new Connection({
  uri: 'amqp://user:password@127.0.0.1:5672/my_vhost',
  // Try to reconnect every 1 second. 
  reconnect: async () => {
    // You have to implement `sleep` function by yourself. 
    await sleep(1000);
 
    return true;
  },
});
const consumer = new Consumer(connection, {
  queue,
  prefetch: 1,
});
await consumer.consume(async (messageID: string, content: any, message) => {
  // You can use the parsed content directly.
  console.log(`Message ID: ${messageID}, Content: ${content.value}`);
 
  // Or, you can do stuff with the `amqp.Message`
  console.log(`Buffer content:`, message.content);
 
  return true;
});
 
setTimeout(async () => {
  // Close consumer and underlying connection by passing `true`.
  await consumer.close(true);
}, 2000);

TODOs

  • Docs
  • Tests

Package Sidebar

Install

npm i trusty-rabbit

Weekly Downloads

6

Version

1.1.0

License

Apache-2.0

Unpacked Size

45.7 kB

Total Files

27

Last publish

Collaborators

  • aaronjan