Chapter

RabbitMQ and Laravel

Wire RabbitMQ into a Laravel 11 app as the queue driver: install the driver, configure the connection, dispatch jobs, run workers, handle failures and route work to named queues.

RabbitMQ Basics 7 Lessons from courses

About this chapter

You have run producers and consumers by hand and learned how RabbitMQ routes, acknowledges and re-delivers messages. Now you'll let a real framework do the plumbing. Laravel already has a queue system, and with one package you can point it at RabbitMQ instead of a database or Redis. You keep writing plain Laravel Jobs; RabbitMQ carries them under the hood.

Lessons from courses

  1. 1 How Laravel queues work See how Laravel queues work - Jobs, connections, drivers - and why you point them at RabbitMQ instead of the database or Redis, with the AMQP hidden.
  2. 2 Installing the Laravel RabbitMQ driver Install the Laravel RabbitMQ queue driver with composer require vyuldashev/laravel-queue-rabbitmq. It registers a rabbitmq connection driver you point config at.
  3. 3 Configuring the connection Add a rabbitmq connection to config/queue.php, set RABBITMQ_HOST, RABBITMQ_PORT, user, password and vhost in .env, then set QUEUE_CONNECTION=rabbitmq.
  4. 4 Dispatching jobs Create a Job with make:job, then dispatch Laravel jobs to RabbitMQ with SendWelcomeEmail::dispatch(user). Route work to named queues with onQueue().
  5. 5 Running workers Run php artisan queue:work rabbitmq to consume jobs from RabbitMQ. Learn how the worker acks messages, the --queue flag, and keeping it alive with Supervisor.
  6. 6 Failed jobs and retries Use --tries and --backoff, store dead jobs in the failed_jobs table, and re-run them with queue:retry. See how Laravel retries map to RabbitMQ redelivery.
  7. 7 Priorities and multiple queues Route jobs to named queues with onQueue, then run queue:work --queue=high,low so a worker drains high-priority queues before lower ones in RabbitMQ.
RabbitMQ Basics Go to course