A lightweight Node.js library that turns MongoDB collections into general-purpose message queues or event logs without additional infrastructure. Version 1.4.1 is stable; released regularly with breaking changes documented. Key differentiators: supports both at-most-once (subscribe) and at-least-once (consume) delivery, configurable retries/visibility/delays, batch publishing, and leverages MongoDB features like indexes, capped collections, transactions, and sharding. No external broker required; designed for event-driven architectures on existing MongoDB deployments. Peer dependency mongodb >=4. Requires Node >=18. Ships TypeScript types.
npm install mongomq2No compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Initializes a MongoDB client, creates a MessageQueue from a collection, publishes a message, and consumes messages in a consumer group.
Upgrade from v0.x? Rewrite usage using the new MessageQueue class. See migration guide in the GitHub repo.
Ensure the consumer callback is async and handles errors internally or throws intentionally to trigger retry.
Replace MessageQueue.create(collection) with new MessageQueue(collection).
Use a regular (uncapped) collection for message queues that need to retain history or use a TTL index to auto-expire messages.
Ensure consumer logic is idempotent or avoid using transactions with publish().
Use consume() with a proper group if you need reliability. Use subscribe() only for non-critical notifications.
Update package.json to include "type": "module" or use dynamic import() in CJS: const { MessageQueue } = await import('mongomq2');Use client.db().collection('name') to get a Collection instance.Remove the _id field from published objects to let MongoDB auto-generate them, or use a unique index for message deduplication and catch the error.
Reduce the callback execution time or increase the visibility timeout in ConsumeOptions (e.g., { visibilityTimeoutMs: 60000 }).