Concept Flow - Queue producers
Start Producer
Create Job Data
Send Job to Queue
Queue Accepts Job
Job Stored in Queue
Producer Ready for Next Job
The producer creates job data and sends it to the queue, which stores it for consumers.
import { InjectQueue } from '@nestjs/bull'; import { Queue } from 'bull'; constructor(@InjectQueue('email') private emailQueue: Queue) {} async sendEmailJob(data) { await this.emailQueue.add(data); }
| Step | Action | Job Data | Queue State | Result |
|---|---|---|---|---|
| 1 | Producer starts | {} | [] | Ready to send job |
| 2 | Create job data | {"to":"user@example.com","subject":"Hi"} | [] | Job data prepared |
| 3 | Add job to queue | {"to":"user@example.com","subject":"Hi"} | [] | Job sent to queue |
| 4 | Queue accepts job | {"to":"user@example.com","subject":"Hi"} | [{id:1, data:{to:"user@example.com",subject:"Hi"}}] | Job stored in queue |
| 5 | Producer ready for next job | {} | [{id:1, data:{to:"user@example.com",subject:"Hi"}}] | Waiting for next job |
| Variable | Start | After Step 2 | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|---|
| jobData | {} | {"to":"user@example.com","subject":"Hi"} | {"to":"user@example.com","subject":"Hi"} | {"to":"user@example.com","subject":"Hi"} | {} |
| queue | [] | [] | [] | [{id:1, data:{to:"user@example.com",subject:"Hi"}}] | [{id:1, data:{to:"user@example.com",subject:"Hi"}}] |
NestJS Queue Producers:
- Inject queue with @InjectQueue('name')
- Create job data object
- Use queue.add(data) to send job
- Job is asynchronously stored in queue
- Producer can send multiple jobs sequentially