Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
redis-smq
Advanced tools
RedisSMQ is a Node.js library for queuing messages (aka jobs) and processing them asynchronously with consumers. Backed by Redis, it allows scaling up your application with ease of use.
2023.01.06
:rocket: RedisSMQ v7.2 is released! Before this release RedisSMQ supported both LIFO queues and Priority queues. With this new release, RedisSMQ now provides FIFO queues as a third option that completes the list of supported queue types. For more details see Queues.
npm install redis-smq-common redis-smq --save
Considerations:
See Configuration for more details.
RedisSMQ provides 3 classes in order to work with the message queue: Message
, Producer
, and Consumer
.
Producers and consumers exchange data using one or multiple queues that may be created using the QueueManager.
A queue is responsible for holding messages which are produced by producers and are delivered to consumers.
const { QueueManager } = require('redis-smq');
const { EQueueType } = require('redis-smq/dist/types');
const config = require('./config')
QueueManager.createInstance(config, (err, queueManager) => {
if (err) console.log(err);
// Creating a LIFO queue
else queueManager.queue.save('test_queue', EQueueType.LIFO_QUEUE, (err) => console.log(err));
})
See Queues for more details.
Message
class is responsible for creating messages that may be published.
A message can carry your application data, sometimes referred to as message payload
, which may be delivered to a consumer to be processed asynchronously.
The message payload can be of any valid JSON data type. It may be a simple text message like Hello world
or a complex data type like {hello: 'world'}
.
const { Message } = require('redis-smq');
const message = new Message();
message
.setBody({hello: 'world'})
.setTTL(3600000)
.setRetryThreshold(5);
The Message
class provides many methods for setting up different message parameters such as message body, message priority, message TTL, etc.
See Message Reference for more details.
A Producer
instance allows to publish a message to a queue.
You can use a single Producer
instance to produce messages, including messages with priority, to one or multiple queues.
Before publishing a message do not forget to set an exchange for the message using setQueue(), setTopic(), or setFanOut(). Otherwise, an error will be returned. See Message Exchanges for more details.
'use strict';
const {Message, Producer} = require('redis-smq');
const producer = new Producer();
producer.run((err) => {
if (err) throw err;
const message = new Message();
message
.setBody({hello: 'world'})
.setTTL(3600000) // message expiration (in millis)
.setQueue('test_queue'); // setting up a direct exchange
message.getId() // null
producer.produce(message, (err) => {
if (err) console.log(err);
else {
const msgId = message.getId(); // string
console.log('Successfully produced. Message ID is ', msgId);
}
});
})
Starting with v7.0.6, before producing messages you need first to run your producer instance.
See Producer Reference for more details.
A Consumer
instance can be used to receive and consume messages from one or multiple queues.
To consume messages from a queue, the Consumer
class provides the consume() method which allows to register a message handler
.
A message handler
is a function that receives a delivered message from a given queue.
Message handlers can be registered at any time, before or after a consumer has been started.
In contrast to producers, consumers are not automatically started upon creation. To start a consumer use the run() method.
To stop consuming messages from a queue and to remove the associated message handler
from your consumer, use the cancel() method.
To shut down completely your consumer and tear down all message handlers, use the shutdown() method.
'use strict';
const { Consumer } = require('redis-smq');
const consumer = new Consumer();
const messageHandler = (msg, cb) => {
const payload = msg.getBody();
console.log('Message payload', payload);
cb(); // acknowledging the message
};
consumer.consume('test_queue', messageHandler, (err) => {
if (err) console.error(err);
});
consumer.run();
Once a message is received, to acknowledge it, you invoke the callback function without arguments, as shown in the example above.
Message acknowledgment informs the MQ that the delivered message has been successfully consumed.
If an error occurred while processing a message, you can unacknowledge it by passing in the error to the callback function.
By default, unacknowledged messages are re-queued and delivered again unless message retry threshold is exceeded.
Delivered messages that couldn't be processed or can not be delivered to consumers are moved to a system generated queue called dead-letter queue (DLQ).
By default, RedisSMQ does not store acknowledged and dead-lettered messages for saving disk and memory spaces, and also to increase message processing performance.
If you need such feature, you can enable it from your configuration object.
See Consumer Reference for more details.
See Performance for more details.
So you are interested in contributing to this project? Please see CONTRIBUTING.md.
7.2.3 (2023-03-26)
FAQs
A simple high-performance Redis message queue for Node.js.
The npm package redis-smq receives a total of 2,411 weekly downloads. As such, redis-smq popularity was classified as popular.
We found that redis-smq demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.