Create Custom Events

In BullMQ, creating a generic distributed realtime event emitter is possible by using our QueueEventsProducer class.

Consumers must use QueueEvents class to subscribe to those events that they are interested in.

const queueName = 'customQueue';
const queueEventsProducer = new QueueEventsProducer(queueName, {
  connection,
});
const queueEvents = new QueueEvents(queueName, {
  connection,
});

interface CustomListener extends QueueEventsListener {
  example: (args: { custom: string }, id: string) => void;
}
queueEvents.on<CustomListener>('example', async ({ custom }) => {
  // custom logic
});

interface CustomEventPayload {
  eventName: string;
  custom: string;
}

await queueEventsProducer.publishEvent<CustomEventPayload>({
  eventName: 'example',
  custom: 'value',
});

Only eventName attribute is required.

Some event names are reserved from Queue Listener API Reference.

Read more:

Last updated

Copyright (c) Taskforce.sh Inc.