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

redis-ccp-queue

v1.1.1

Published

A simple and lightweight Competing Consumers Pattern Queue

Downloads

14

Readme

redis-ccp-queue

A simple and lightweight Competing Consumers Pattern Queue. Built with node.js and Redis (just about 100 lines of code).

NPM Version NPM Downloads MIT license

Enable multiple concurrent consumers to process messages received on the same messaging channel.

Quick Start

Installation

$ npm install redis-ccp-queue --save

Usage

Create a new Producer

'use strict';

const ccpq = require('redis-ccp-queue');

let p = new ccpq.Producer('QueueKey');    // create new producer instance

...
p.push('message');                        // push a string message to queue
p.push({ name: 'object', num: 42 });      // push an object to queue
...

Create a new Consumer

When creating a consumer you pass a callback function, that is called every time, when a new message is available from the queue:

'use strict';

const ccpq = require('redis-ccp-queue');

let c = new ccpq.Consumer('QueueKey', (data, done) => {  // create new consumer instance
  handleMessage(data, done)
});

// This is the function (callback) that actually handles the messages from the queue.
// Put your worker-code inside this function.
function handleMessage(data, done) {
  console.log('CONSUMER:');
  console.log(data);
  done();                                  // call done() when message is handled
}

Within your callback, you need to call the done() function, to let the consumer know, that the you are finished with the message handling. The name of this function can be changed. just choose your preferred name and pass it as a second parameter name to the callback function.

Examples

I have attached a producer as well as a consumer in the examples folder. To see the queue in acton, open three command line instances. Be sure to have redis installed and running.

In the first command line window lauch the producer with

npm run producer

Then in the second command line window launch the first consumer with

npm run consumer

and immediately after then in the third command line window launch the second consumer with

npm run consumer

You should now see both consumers pulling from the queue. Each message is only handled by one of the consumers. I used some setTimeout functions to simulate longer message handling. I also added a timeout to disconnect the consumers from Redis after a certain period.

Core concept

The competing consumers pattern enables multiple consumers to pull and handle messages from the same queue, with the guarantee that each message is consumed once only. This pattern also allows multiple producers or senders to push messages to the same single queue. The queue is implemented as a FIFO (first in - first out queue):

Application instances                           Consumer service instances pool
generating messages                                         processing messages


--------------
| Producer 1 | --->
--------------

--------------                                                   --------------
| Producer 2 | --->                                         ---> | Consumer 1 |
--------------                                                   --------------

--------------       -------------------------------------       --------------
| Producer 3 | --->  | ------ ------ ------       ------ |  ---> | Consumer 2 |
--------------       | | M5 | | M4 | | M3 | ....> | M1 | |       --------------
                     | ------ ------ ------       ------ |
       .             -------------------------------------            .
       .                      Message Queue (FIFO)                    .
       .                                                              .

--------------                                                   --------------
| Producer x | --->                                         ---> | Consumer n |
--------------                                                   --------------

--------------
| Producer y | --->
--------------

Use this approach when:

  • The workload for an application or task is divided into tasks that can run asynchronously.
  • Tasks are independent and can run in parallel.
  • You need a scalable solution, because the volume of work is highly variable.
  • Your solution must provide high availability.

Reference

Producer

| function | Comments | | -------------- | ------- | | new ccpq.Producer(queueName [, options]) | expects an queue name and (optional) Redis Options *) | | push(data) | push data to the queue. This can be either a string or an JSON object | | shutdown() | disconnects safely from Redis |

Consumer

| function | Comments | | -------------- | ------- | | new ccpq.Consumer(queueName, callback [, options]) | expects a queue name, your callback function (where you handle/consume the data from the queue) and (optional) Redis Options *) | | shutdown() | disconnects safely from Redis |

*) Connect to Redis

When a new Producer or Consumer instance is created, a connection to Redis will be created at the same time. You can specify which Redis to connect to by:

new Producer('QueueKey')                        // Connect to Redis Server at 127.0.0.1:6379
new Producer('QueueKey', 6380)                  // 127.0.0.1:6380
new Producer('QueueKey', '/tmp/redis.sock')
new Producer('QueueKey', {
  port: 6379,                                   // Redis port
  host: '127.0.0.1',                            // Redis host
  family: 4,                                    // 4 (IPv4) or 6 (IPv6)
  password: 'auth',
  db: 0
})

You can also specify connection options as a redis:// URL:

// Connect to 127.0.0.1:6380, db 4, using password "authpassword":
new Redis('redis://:[email protected]:6380/4')

See ioredis API Documentation for all available options.

Known Issues

This is the initial version of this package. At the moment, it really does, what I expected is to do. But I am sure, there is quite a lot of room for improvement. I am happy to discuss any comments and suggestions. Please feel free to contact me if you see any possibility of improvement!

For the next major version I plan to implement a more reliable version where an additional list is used to track messages in transit. If a processes fails to deliver with in a specified amount of time, an item could be moved back to the original queue for delivery.

Version history

| Version | Date | Comment | | -------------- | -------------- | -------- | | 1.1.1 | 2019-10-14 | documentation update | | 1.1.0 | 2019-10-14 | code cleanup, dependency bump | | 1.0.5 | 2016-03-03 | tiny bug fix | | 1.0.4 | 2016-03-03 | correct termination of consumer on SIGINT, SIGTERM | | 1.0.3 | 2016-03-01 | changed .gitignore | | 1.0.2 | 2016-03-01 | bug in dependencies | | 1.0.1 | 2016-02-29 | typos | | 1.0.0 | 2016-02-29 | initial release |

Comments

If you have ideas or comments, please do not hesitate to contact me.

Happy queueing!

Sincerely,

Sebastian Hildebrandt, +innovations

Credits

Written by Sebastian Hildebrandt sebhildebrandt

Trademarks

Node.js is a trademark of Joyent Inc., Redis, and the Redis logo are the trademarks of Salvatore Sanfilippo in the U.S. and other countries. Linux is a registered trademark of Linus Torvalds. All other trademarks are the property of their respective owners.

License MIT license

The MIT License (MIT)

Copyright © 2016-2019 Sebastian Hildebrandt, +innovations.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Further details see LICENSE file.