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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@therms/future-queue

v3.0.1

Published

A MongoDB queue service for Node.js with scheduling, updating/removing queued items and retries

Readme

FutureQueue

Node.js 20+ License: MIT

FutureQueue is a lightweight scheduling service built on Node.js and MongoDB that allows you to enqueue messages for future processing. It’s ideal for scenarios where you need to defer tasks—such as sending reminder emails or processing deferred jobs—while also handling retries, cancellations, and failures via a dead-letter queue.

Note: FutureQueue requires Node.js 20+ and a MongoDB instance.


Table of Contents


Features

  • Schedule Future Messages: Enqueue messages to be processed at a specified future time.
  • Multiple Consumers: Process messages concurrently with distributed consumer instances.
  • Retry Mechanism: Automatically retry processing failed messages.
  • Dead Letter Queue: Move messages that exceed retry limits to a dedicated dead-letter queue.
  • Update & Cancellation: Retrieve, update, or cancel scheduled messages using unique keys.
  • Progress Tracking: Use helper functions to update and monitor the progress of message processing.
  • Message Locking: Prevent duplicate processing by locking messages during handling.
  • Idle Queue Notifications: Optionally receive notifications when the queue is idle.

Requirements

  • Node.js: Version 20 or later.
  • MongoDB: A running MongoDB instance (local or remote).

Installation

Install FutureQueue via npm:

npm install future-queue

Usage

FutureQueue provides two main classes:

  • Producer: For adding, updating, retrieving, and canceling messages.
  • Consumer: For processing messages when they become ready.

Producer Example

The Producer is used to schedule messages. Each message is defined by a unique key and contains custom data. You can specify when the message should be processed, set an expiration, or configure retry options.

import { Producer } from 'futurequeue';

const queueName = 'email-reminders';
const producer = new Producer(queueName, {
  mongo: {
    uri: 'mongodb://localhost:27017/your-db-name', // Or pass a connected MongoClient instance
  },
});

// Schedule a reminder email to be sent in 3 days
const threeDaysFromNow = new Date();
threeDaysFromNow.setDate(threeDaysFromNow.getDate() + 3);

await producer.add(
  {
    key: 'reminder-email-user-123',
    data: { email: '[email protected]', subject: 'Reminder', body: 'Your subscription expires soon!' },
  },
  {
    readyTime: threeDaysFromNow,
    retries: 2, // Maximum retry attempts before moving to dead-letter queue
  },
);

// Retrieve the scheduled message by its key
const message = await producer.getByKey('reminder-email-user-123');
console.log('Retrieved message:', message);

// Update message data if needed
await producer.updateByKey('reminder-email-user-123', {
  data: { email: '[email protected]', subject: 'Updated Reminder', body: 'Please check your subscription!' },
});

// Cancel the scheduled message
await producer.cancelByKey('reminder-email-user-123');

// Gracefully shut down the producer
await producer.shutdown();

Consumer Example

The Consumer listens for messages that are ready to be processed. You define a handler function to process each message. The handler receives helper functions to extend the processing lock and update progress.

import { Consumer } from 'futurequeue';

const queueName = 'email-reminders';
const consumer = new Consumer(queueName, {
  mongo: {
    uri: 'mongodb://localhost:27017/your-db-name',
  },
});

// Define the message processing handler
consumer.processMessages(async (message, { extendMsgHandlingLock, updateMsgHandlerProgress }) => {
  // Simulate processing by updating progress
  await updateMsgHandlerProgress({ percentComplete: 50 });
  
  // Process the message (e.g., send an email)
  console.log(`Processing message with key: ${message.key}`, message.data);
  
  // Optionally extend the processing lock if processing takes longer than expected
  // await extendMsgHandlingLock();
  
  // Mark the processing as complete
  await updateMsgHandlerProgress({ percentComplete: 100 });
});

// Optionally, register an idle callback
consumer.processMessages(
  async (message) => {
    console.log(`Processed: ${message.key}`);
  },
  {
    onQueueIdle: () => {
      console.log('The queue is currently idle.');
    },
  },
);

// When your application is shutting down, gracefully close the consumer
await consumer.shutdown();

API Reference

Producer

new Producer(queueName: string, options: ProducerOptions)

  • queueName: Unique name for your queue.
  • options: Configuration options, including MongoDB connection settings.

Methods

add(message: QueueMessage | QueueMessage[], options?: { readyTime?: Date; expireTime?: Date; retries?: number; })

Adds one or more messages to the queue.

  • readyTime (optional): When the message should be available for processing.
  • expireTime (optional): When the message expires.
  • retries (optional): Number of retry attempts allowed.

getByKey(key: string)

Retrieves a scheduled message by its unique key.

cancelByKey(key: string)

Cancels (deletes) a scheduled message from the queue.

updateByKey(key: string, update: Partial<QueueMessage>)

Updates properties of a scheduled message.

totalReadyMessages()

Returns the count of messages that are ready to be processed immediately.

totalFutureMessages()

Returns the count of messages scheduled for future processing.

shutdown()

Gracefully shuts down the producer and releases any held resources.


Consumer

new Consumer(queueName: string, options: ConsumerOptions)

  • queueName: The name of the queue to process.
  • options: Configuration options, including MongoDB settings, logging, and message-handling configurations.

Methods

processMessages(handler: (message: ReadyQueueMessage, helpers?: MessageProcessingHelpers) => Promise<void>, options?: { onQueueIdle?: () => void; })

Registers a message processing handler.

The handler receives:

  • message: The message object.
  • helpers: An object containing:
    • extendMsgHandlingLock(): Function to extend the lock on the message.
    • updateMsgHandlerProgress(progress: any): Function to update the progress of message processing.
  • onQueueIdle (optional): Callback invoked when there are no messages left to process.

totalFutureMessages()

Returns the count of messages still scheduled for future processing.

shutdown()

Gracefully shuts down the consumer.


Use Cases

Scheduled Email Reminders

Schedule reminder emails to be sent at a future date. For example, schedule an email to be sent 3 days before a user's subscription expires.

Deferred Task Processing

Enqueue background tasks—such as data processing, notifications, or report generation—to run during off-peak hours.

Retry and Error Handling

Automatically retry tasks that fail during processing. If a task fails after the configured number of retries, it is moved to a dead-letter queue for further investigation.

Distributed Workload Processing

Scale your application by deploying multiple consumer instances. The locking mechanism ensures that each message is processed only once, even when multiple consumers are polling the queue.