To establish a connection in BullMQ, you primarily need to create an instance of a queue by using the Queue
class provided by the BullMQ library. This involves setting up a Redis server as BullMQ is backed by this. Here's a basic example:
const { Queue } = require('bullmq'); // You need to have redis server running const connectionOpts = { host: '127.0.0.1', port: 6379, password: 'your_password', // If set in your Redis server }; const myQueue = new Queue('my-queue', { connection: connectionOpts }); // Now you can add jobs to the queue myQueue.add('myJob', { foo: 'bar' });
In the above code snippet,
Queue
module from the bullmq
package.connectionOpts
object for our Redis connection.Queue
named 'my-queue' and pass the connection options to it.After this, we've successfully connected to our Redis instance and have a BullMQ queue ready to use. You can then proceed to add jobs to the queue using the add
method on the queue instance (like myQueue.add('myJob', { foo: 'bar' });
).
Please make sure the Redis server is running before initiating the Queue. The host and port are the address where the Redis server is running.
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.