### Install mongo-message-queue Source: https://github.com/wonderlic/mongo-message-queue/blob/master/README.md Installs the mongo-message-queue package using npm. This is the first step to integrate the message queue into your Node.js project. ```bash npm install mongo-message-queue --save ``` -------------------------------- ### Instantiate MessageQueue Source: https://github.com/wonderlic/mongo-message-queue/blob/master/README.md Demonstrates how to require and create an instance of the MessageQueue class in a Node.js application. This sets up the basic message queue object. ```javascript const MessageQueue = require('mongo-message-queue'); const mQueue = new MessageQueue(); ``` -------------------------------- ### Configure Database Connection Source: https://github.com/wonderlic/mongo-message-queue/blob/master/README.md Configures the message queue to use a specific MongoDB database connection. The `databasePromise` property should be set to a function returning a promise that resolves with a MongoDB database connection. ```javascript const MongoClient = require('mongodb').MongoClient; const mongoUri = ...; const mongoOptions = ...; mQueue.databasePromise = function() { // Return a promise to return a mongo database connection here... return MongoClient.connect(mongoUri, mongoOptions); }; ``` -------------------------------- ### Enqueue and Process Message Immediately Source: https://github.com/wonderlic/mongo-message-queue/blob/master/README.md Enqueues a message and attempts to process it immediately using a locally registered worker. If no local worker is available, the message will be processed later when a worker becomes available. ```javascript mQueue.enqueueAndProcess('doSomething', {id: 123, status: 'done'}); ``` -------------------------------- ### Enqueue Message for Processing Source: https://github.com/wonderlic/mongo-message-queue/blob/master/README.md Enqueues a message to be processed by a registered worker. Messages are typically processed immediately by available workers. An optional third parameter can specify a future `nextReceivableTime`. ```javascript mQueue.enqueue('doSomething', {id: 123, status: 'done'}); mQueue.enqueue('doSomething', {id: 123, status: 'done'}, {nextReceivableTime: new Date(Date.now() + 30 * 1000)}); ``` -------------------------------- ### Register Worker for Message Processing Source: https://github.com/wonderlic/mongo-message-queue/blob/master/README.md Registers a worker function to process messages of a specific type. The worker function receives a `queueItem` object containing the message and retry information. It should return a promise resolving to 'Completed', 'Rejected', or 'Retry'. ```javascript mQueue.registerWorker('doSomething', function (queueItem) { // Return a promise to do something here... return database.collection('somecollection') .updateOne({_id: queueItem.message.id}, {status: queueItem.message.status}) .then(function (result) { return 'Completed'; }) .catch(function (err) { queueItem.releasedReason = err.message; if ((queueItem.retryCount || 0) < 5) { queueItem.nextReceivableTime = new Date(Date.now() + 30 * 1000); // Retry after 30 seconds... return 'Retry'; } else { queueItem.rejectionReason = 'Gave up after 5 retries.'; return 'Rejected'; } }); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.