How to use Beanstalkd as the queue driver in Laravel?

To use Beanstalkd as the queue driver in Laravel, you need to follow these steps:

  1. Install the required packages: Open a terminal and navigate to your Laravel project directory. Run the following command to install Beanstalkd and the Laravel Beanstalkd package.
composer require pda/pheanstalk laravel/queue
  1. Configure the queue driver: Open the .env file in your Laravel project and update the QUEUE_CONNECTION value to beanstalkd.
QUEUE_CONNECTION=beanstalkd
  1. Update the config/queue.php file: Laravel comes with a default configuration file for the queue. Open the config/queue.php file and update the beanstalkd section with the following configuration:
'beanstalkd' => [ 'driver' => 'beanstalkd', 'host' => '127.0.0.1', 'queue' => 'default', 'ttr' => 60, ],

Here, you can modify the host, queue, and ttr (Time-To-Run) values as per your requirements.

  1. Create a queue worker: Laravel provides an artisan command to run queue workers. Open a terminal and navigate to your Laravel project directory. Run the following command to create a queue worker:
php artisan queue:work --queue=your-queue-name

Replace your-queue-name with the name of the queue you want to process. If you have multiple queues, separate them with commas.

  1. Queue jobs: You can now start queuing jobs to Beanstalkd. To queue a job, you can use the dispatch helper function or the Jobs class in Laravel. Here's an example:
dispatch(function () { // Your job logic here });

Or

dispatch(new YourJobClass());

That's it! Laravel will connect to Beanstalkd and process the queued jobs in the background. You can check the Laravel logs or the Beanstalkd logs for any errors or job-specific information.