Skip to content

svtslv/nestjs-bullmq

Repository files navigation

NestJS BullMQ

NPM Version Package License

Table of Contents

Description

Integrates BullMQ with Nest

Installation

npm install nestjs-bullmq bullmq

You can also use the interactive CLI

npx nestjs-modules

Examples

BullMQModule.forRoot(options, connection?)

import { Module } from '@nestjs/common';
import { BullMQModule } from 'nestjs-bullmq';
import { AppController } from './app.controller';

@Module({
  imports: [
    BullMQModule.forRoot({
      name: 'QueueName',
      config: {
        // url: 'redis://:password@localhost:6379',
        connection: { host: 'localhost', port: 6379 },
      },
    }),
  ],
  controllers: [AppController],
})
export class AppModule {}

BullMQModule.forRootAsync(options, connection?)

import { Module } from '@nestjs/common';
import { BullMQModule } from 'nestjs-bullmq';
import { AppController } from './app.controller';

@Module({
  imports: [
    BullMQModule.forRootAsync({
      name: 'QueueName',
      useFactory: () => ({
        config: {
          // url: 'redis://:password@localhost:6379',
          connection: { host: 'localhost', port: 6379 }, 
        },
      }),
    }),
  ],
  controllers: [AppController],
})
export class AppModule {}

InjectBullMQ(queueName?, connection?)

import { Controller, Get, } from '@nestjs/common';
import { InjectBullMQ, BullMQ } from 'nestjs-bullmq';

@Controller()
export class AppController {
  constructor(
    @InjectBullMQ('QueueName') private readonly bullMQ: BullMQ,
  ) {}

  @Get()
  async getHello() {
    this.bullMQ.process(async job => {
      console.log('process', job); 
    })
    this.bullMQ.queue.add('myJobName', { foo: 'bar' });
    // this.bullMQ.queueEvents.on();
    // this.bullMQ.queueScheduler.on();
  }
}

License

MIT