Git Product home page Git Product logo

star-amqp's Introduction

Star AMQP

Installation

npm install --save star-amqp

Usage

Client

const client = new AMQP({
  url: 'connectionString',
  certificate: 'certificateString',
});

Producer

const myQueue = new client.Queue('test');
myQueue.send({ some: 'stuff' });

Consumers (eg. workers)

const myQueue = new client.Queue('test', (data, info) => {
  // `data` is the message payload
  // `info` holds all info about the MQ message (retry count, etc)
  console.log(JSON.stringify(data));
  return true;
});
myQueue.send({ some: 'stuff' });

Creating queues and attaching workers

The following code will create a queue called 'test-queue', and a retry queue called 'test-queue-retry'. The attached worker will process 1 message at the time. If a message is Nacked (eg. failed) by the worker it will be retried again in 60 seconds. Once a message is Acked (successfully processed) by the worker it is removed from the queue.

// Instantiate the client
const client = new AMQP({ url: 'amqp://your-rabbitmq-connection-string' });

// Queue options
const opts = { retryDelay: 60000, prefetch: 100 };

// Instantiate a queue and attach a worker
const testQueue = new client.Queue('test-queue', async message => {
  // Print the payload
  console.log('processing ->', JSON.stringify(message));

  // Ack
  return true;

  // Nack - the message will be retried in 60 seconds
  // return false

  // Nack with a delay - for handling rate limits
  // return 1000
});

// Sending some messages to the queue. The message can be any JSONifyable type.
testQueue.send({ some: 'payload' });
testQueue.send({ another: 'payload' });
testQueue.send('string as a payload');
testQueue.send(['an', 'array']);
testQueue.send(1);

Options

You can provide an object literal with options to the Queue constructor to modify the default behavior of a queue.

With the options below the attached worker will process up to 100 messages simultaneously and any failed messages will be retried in 30 seconds.

const opts = {
  // Concurrency, max number of simultaneously processed messages (default: 1)
  prefetch: 100,

  // The delay in milliseconds before Nacked messages are retried (default: 60000)
  retryDelay: 30000,
};

# Instantiate the queue and attach the worker with the new options.
new client.Queue('test-queue', opts, async message => true);

Batch send messages

TBD

Retry queues

TBD

Contributing

Contributions are encouraged.

Before issuing a pull request, please run the following checks.

npm run lint
npm run flow
URI='amqp://username:password@host/vhost' npm run test

If you need a RabbitMQ server to test against, CloudAMQP offers free RabbitMQ vhosts.

Changelog

v0.0.6 - Message details are passed to worker (#1) Thanks @cyrillegin

v0.0.7 - Upgrade amqplib dependency to v0.5.3 (#5)

v0.0.9 - Updated babel versions and config (#18)

star-amqp's People

Contributors

cyrillegin avatar dependabot[bot] avatar nbutler77 avatar senormeow avatar shiftycow avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

visgence

star-amqp's Issues

"Unknown Error" thrown by Node.js

We're getting an error message similar to the one described here; trace looks like:

events.js:167
throw er; // Unhandled 'error' event
^
Error: Unexpected close
at succeed (/home/centos/flirmachinelearning/node_modules/amqplib/lib/connection.js:271:13)
at onOpenOk (/home/centos/flirmachinelearning/node_modules/amqplib/lib/connection.js:253:5)
at /home/centos/flirmachinelearning/node_modules/amqplib/lib/connection.js:166:32
at /home/centos/flirmachinelearning/node_modules/amqplib/lib/connection.js:160:12
at Socket.recv (/home/centos/flirmachinelearning/node_modules/amqplib/lib/connection.js:498:12)
at Object.onceWrapper (events.js:273:13)
at Socket.emit (events.js:182:13)
at Socket.EventEmitter.emit (domain.js:441:20)
at emitReadable_ (_stream_readable.js:534:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
at Connection.emit (events.js:182:13)
at Connection.EventEmitter.emit (domain.js:441:20)
at Connection.C.onSocketError (/home/centos/flirmachinelearning/node_modules/amqplib/lib/connection.js:352:10)
at Socket.emit (events.js:187:15)
at Socket.EventEmitter.emit (domain.js:441:20)
at endReadableNT (_stream_readable.js:1094:12)
at process._tickCallback (internal/process/next_tick.js:63:19)

It seems like updating amqplib to 0.5.3 may address the issue. Can you do that?

Thanks,

Neal Butler

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.