Registry / messaging / mongo-message-queue

mongo-message-queue

JSON →
library1.1.0jsnpmunverified

A promise-based message queue for Node.js backed by MongoDB. Version 1.1.0 is the latest stable release. It provides a simple worker pattern where messages of a given type are processed by registered handlers, with built-in retry and rejection support. Compatible with MongoDB driver versions 4 through 6 and MongoDB server versions 5 through 7. Uses polling (default 1 second) to check for new messages. Differentiators: lightweight, no external dependencies besides MongoDB, and simple API.

npm install mongo-message-queue
INSTALL
IMPORT
SIG · MONGO-MESSAGE-QUEU
M
mongo-message-queue
messagingjavascriptv1.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MessageQueue
✓ const MessageQueue = require('mongo-message-queue');
✗ import MessageQueue from 'mongo-message-queue';
Package is CommonJS-only; does not support ESM imports.
MessageQueue
✓ const { MessageQueue } = require('mongo-message-queue');
✗ const { MessageQueue } = require('mongo-message-queue');
The package exports a single constructor function directly, not as named export. So destructuring will give undefined.
MongoClient
✓ const { MongoClient } = require('mongodb');
✗ const MongoClient = require('mongodb').MongoClient;
Both forms work with MongoDB driver v4+, but destructuring is modern.

Shows how to instantiate MessageQueue, set up a MongoDB connection, register a worker with retry logic, and enqueue a message.

const MessageQueue = require('mongo-message-queue'); const { MongoClient } = require('mongodb'); const mQueue = new MessageQueue(); const mongoUri = process.env.MONGO_URI || 'mongodb://localhost:27017'; mQueue.databasePromise = async () => { const client = new MongoClient(mongoUri); await client.connect(); return client.db('messageQueue'); }; mQueue.registerWorker('sendEmail', async (queueItem) => { const { to, subject } = queueItem.message; try { // Simulate sending email console.log(`Sending email to ${to}: ${subject}`); return 'Completed'; } catch (err) { queueItem.releasedReason = err.message; if ((queueItem.retryCount || 0) < 3) { queueItem.nextReceivableTime = new Date(Date.now() + 10000); return 'Retry'; } else { queueItem.rejectionReason = 'Max retries exceeded'; return 'Rejected'; } } }); await mQueue.enqueue('sendEmail', { to: 'user@example.com', subject: 'Hello' }); console.log('Message enqueued');
Debug
Known issues
breakingMongoDB driver peer dependency changed from 3.x to >= 4.0.0 in version 1.0.0.
fix
Update MongoDB driver to version 4.0.0 or higher.
affects: <1.0.0
deprecatedThe .databasePromise method expects a function returning a promise to a database object, not a client.
fix
Ensure the promise resolves to a Db object (from client.db()), not a MongoClient.
affects: >=1.0.0
gotchaPolling starts automatically on worker registration. Call mQueue.stopPolling() to stop.
fix
Use mQueue.stopPolling() when you no longer need to process messages.
affects: >=0.1.0
gotchaThe queueItem.retryCount is only available after the first retry; it is undefined on first attempt.
fix
Use (queueItem.retryCount || 0) to safely get the count.
affects: >=0.1.0
gotchaEnqueuing a message requires the worker to be registered first; otherwise, the message will be stored but never processed.
fix
Register all workers before enqueuing messages.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: mQueue.databasePromise is not a function
The .databasePromise property was not set or is not a function returning a promise.
fix
Set mQueue.databasePromise = async () => { return await client.db('queue'); };
MongoError: no primary server available
MongoDB connection string or cluster is not reachable.
fix
Check MONGO_URI environment variable or ensure MongoDB server is running.
TypeError: queueItem.message is undefined
The enqueued message had no payload or was malformed.
fix
Ensure enqueue is called with a valid message object.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies
mongodbrequiredPeer dependency required for database connection and operations.
Agent activity
7 hits · last 30 days
node
6
OpenAI (training)
1
Resources
mongo-message-queue — npm install mongo-message-queue · libregistry