[!NOTE]
This project is a Community Project.The project is maintained and supported by the community. Upstash may contribute but does not officially support or assume responsibility for it.
A simple, fast, robust, stream-based message queue for Node.js, backed by Upstash Redis, inspired by AWS SQS.
import { Redis } from "@upstash/redis";
import { Queue } from "@upstash/queue";
const queue = new Queue({ redis: new Redis() });
await queue.sendMessage({ hello: "world1" });
const message1 = await queue.receiveMessage<{ hello: "world1" }>();
expect(message1?.body).toEqual({ hello: "world1" });
@upstash/queue is a Node.js library that provides a simple and efficient way to implement a message queue system using Redis streams. It offers features such as message sending, receiving, automatic message verification, and concurrency control. This library is particularly useful for building distributed systems and background job processing.
Upstash Queue brings the simplicity and performance of Redis streams to Node.js developers, making it easy to integrate a robust message queue system into their applications. Whether you're working on distributed systems, background job processing, or other asynchronous workflows, Upstash Queue provides essential features such as message sending, receiving, automatic verification, and concurrency control.
Key Features:
To start using Upstash Queue, install the library via npm:
npm install @upstash/queue
import Redis from "@upstash/redis";
import { Queue } from "@upstash/queue";
const redis = new Redis();
const queue = new Queue({ redis });
const payload = { key: "value" };
const delayInSeconds = 0; // Set to 0 for immediate delivery.
const streamId = await queue.sendMessage(payload, delayInSeconds);
const pollingForNewMessages = 1000; // Set to 0 for non-blocking, otherwise, it will try to get a message then fail if none is available.
const receivedMessage = await queue.receiveMessage(pollingForNewMessages);
console.log("Received Message:", receivedMessage);
const streamId = "some_stream_id";
const verificationStatus = await queue.verifyMessage(streamId);
console.log("Verification Status:", verificationStatus);
When initializing the Queue instance, you can provide various configuration options:
const queueConfig = {
redis: new Redis(),
queueName: "MyCustomQueue",
concurrencyLimit: 2,
autoVerify: false,
consumerGroupName: "MyConsumers",
visibilityTimeout: 60000, // 1 minute.
};
const customQueue = new Queue(queueConfig);
const queue = new Queue({ redis });
await queue.sendMessage({ hello: "world1" });
await delay(100);
await queue.sendMessage({ hello: "world2" });
await delay(100);
await queue.sendMessage({ hello: "world3" });
const message1 = await queue.receiveMessage<{ hello: "world1" }>(); // Logs out { hello: "world1" }.
const message2 = await queue.receiveMessage<{ hello: "world2" }>(); // Logs out { hello: "world2" }.
const message3 = await queue.receiveMessage<{ hello: "world3" }>(); // Logs out { hello: "world3" }.
const fakeValue = randomValue();
const queueName = "app-logs";
const producer = new Queue({ redis, queueName });
const consumer = new Queue({
redis: new Redis(),
queueName,
});
await producer.sendMessage(
{
dev: fakeValue,
},
2000
);
const receiveMessageRes = await consumer.receiveMessage<{
dev: string;
}>(5000);
const queue = new Queue({ redis, autoVerify: false });
await queue.sendMessage({ hello: "world" });
const message = await queue.receiveMessage<{ hello: "world" }>(); // Logs out { hello: "world" }.
if (message) {
await queue.verifyMessage(message.streamId); //Logs out "VERIFIED" or "NOT VERIFIED".
}
If concurrencyLimit is not set, one of the receiveMessage() will throw. You need to explicitly set the concurrencyLimit. Default is 1.
const queue = new Queue({
redis: new Redis(),
queueName: randomValue(),
concurrencyLimit: 2,
});
await queue.sendMessage({
dev: randomValue(),
});
await queue.sendMessage({
dev: randomValue(),
});
await Promise.all([queue.receiveMessage(), queue.receiveMessage()]);
1,741 followers · starred Apr 2024
68 followers · starred May 2024
[!NOTE]
This project is a Community Project.The project is maintained and supported by the community. Upstash may contribute but does not officially support or assume responsibility for it.
A simple, fast, robust, stream-based message queue for Node.js, backed by Upstash Redis, inspired by AWS SQS.
import { Redis } from "@upstash/redis";
import { Queue } from "@upstash/queue";
const queue = new Queue({ redis: new Redis() });
await queue.sendMessage({ hello: "world1" });
const message1 = await queue.receiveMessage<{ hello: "world1" }>();
expect(message1?.body).toEqual({ hello: "world1" });
@upstash/queue is a Node.js library that provides a simple and efficient way to implement a message queue system using Redis streams. It offers features such as message sending, receiving, automatic message verification, and concurrency control. This library is particularly useful for building distributed systems and background job processing.
Upstash Queue brings the simplicity and performance of Redis streams to Node.js developers, making it easy to integrate a robust message queue system into their applications. Whether you're working on distributed systems, background job processing, or other asynchronous workflows, Upstash Queue provides essential features such as message sending, receiving, automatic verification, and concurrency control.
Key Features:
To start using Upstash Queue, install the library via npm:
npm install @upstash/queue
import Redis from "@upstash/redis";
import { Queue } from "@upstash/queue";
const redis = new Redis();
const queue = new Queue({ redis });
const payload = { key: "value" };
const delayInSeconds = 0; // Set to 0 for immediate delivery.
const streamId = await queue.sendMessage(payload, delayInSeconds);
const pollingForNewMessages = 1000; // Set to 0 for non-blocking, otherwise, it will try to get a message then fail if none is available.
const receivedMessage = await queue.receiveMessage(pollingForNewMessages);
console.log("Received Message:", receivedMessage);
const streamId = "some_stream_id";
const verificationStatus = await queue.verifyMessage(streamId);
console.log("Verification Status:", verificationStatus);
When initializing the Queue instance, you can provide various configuration options:
const queueConfig = {
redis: new Redis(),
queueName: "MyCustomQueue",
concurrencyLimit: 2,
autoVerify: false,
consumerGroupName: "MyConsumers",
visibilityTimeout: 60000, // 1 minute.
};
const customQueue = new Queue(queueConfig);
const queue = new Queue({ redis });
await queue.sendMessage({ hello: "world1" });
await delay(100);
await queue.sendMessage({ hello: "world2" });
await delay(100);
await queue.sendMessage({ hello: "world3" });
const message1 = await queue.receiveMessage<{ hello: "world1" }>(); // Logs out { hello: "world1" }.
const message2 = await queue.receiveMessage<{ hello: "world2" }>(); // Logs out { hello: "world2" }.
const message3 = await queue.receiveMessage<{ hello: "world3" }>(); // Logs out { hello: "world3" }.
const fakeValue = randomValue();
const queueName = "app-logs";
const producer = new Queue({ redis, queueName });
const consumer = new Queue({
redis: new Redis(),
queueName,
});
await producer.sendMessage(
{
dev: fakeValue,
},
2000
);
const receiveMessageRes = await consumer.receiveMessage<{
dev: string;
}>(5000);
const queue = new Queue({ redis, autoVerify: false });
await queue.sendMessage({ hello: "world" });
const message = await queue.receiveMessage<{ hello: "world" }>(); // Logs out { hello: "world" }.
if (message) {
await queue.verifyMessage(message.streamId); //Logs out "VERIFIED" or "NOT VERIFIED".
}
If concurrencyLimit is not set, one of the receiveMessage() will throw. You need to explicitly set the concurrencyLimit. Default is 1.
const queue = new Queue({
redis: new Redis(),
queueName: randomValue(),
concurrencyLimit: 2,
});
await queue.sendMessage({
dev: randomValue(),
});
await queue.sendMessage({
dev: randomValue(),
});
await Promise.all([queue.receiveMessage(), queue.receiveMessage()]);
1,741 followers · starred Apr 2024
68 followers · starred May 2024