Announcing BullMQ for PHP
Read more:
BullMQ now has a client for PHP. With it, a PHP application can add jobs to BullMQ queues, and workers written in Node.js, Python or Elixir process them.
A queue client, not a worker
The PHP package implements the producer side of BullMQ: the Queue class. It does not include a worker. PHP usually runs for the length of a request, which makes it a poor fit for processes that wait for jobs for hours, while the other BullMQ libraries are built for exactly that.
This split matches how many teams already work: the web application is written in PHP, and the slow or heavy work, such as sending emails, generating reports or calling other services, happens somewhere else. Now that work can go through the same BullMQ queues as the rest of your system.
Adding jobs from PHP
use BullMQ\Queue;
$queue = new Queue('email-queue');
$job = $queue->add('send-email', [
'to' => 'user@example.com',
'subject' => 'Welcome!',
]);
// A delayed job that is retried up to 3 times
$queue->add('reminder', $data, [
'delay' => 60000,
'attempts' => 3,
'backoff' => ['type' => 'exponential', 'delay' => 1000],
]);
The client supports the job options you know from the other libraries: delays, priorities, custom job IDs, retries with backoff, and automatic removal of finished jobs. You can add jobs in bulk with addBulk, add a job as the child of an existing parent job, read jobs and job counts by state, and pause and resume a queue.
A worker in Node.js picks the jobs up as usual:
import { Worker } from 'bullmq';
const worker = new Worker('email-queue', async job => {
await sendEmail(job.data.to, job.data.subject);
}, { connection: { host: 'localhost', port: 6379 } });
Installation
The package is distributed from the BullMQ repository. Add the repository to your project and require the package with Composer:
composer config repositories.bullmq vcs https://github.com/taskforcesh/bullmq
composer require taskforcesh/bullmq-php:dev-master
It requires PHP 8.1 or newer and Redis 5.0 or newer (6.2 or newer is recommended).
The same scripts as every other BullMQ library
The PHP client runs the same Lua scripts as BullMQ for Node.js, Python and Elixir, so a job added from PHP is stored exactly like a job added from any of them. There is no bridge or separate format to maintain.
The guide is in the BullMQ documentation, and the source lives in the BullMQ repository. If you run into anything, please open an issue.
Update: releases of the PHP client are now published as a zip file on the releases page, and its jobs can also be processed by .NET and Rust workers. See BullMQ for PHP for the current instructions.