[Part 2] – Use beanstalkd messaging queue with tomatocart

This is the second article in a series I’m writing about using beanstalkd messaging queue with Tomatocart. If you haven’t already, I recommend you read the first article first.

Now that you understand the basic concept of beanstalkd and install it into your web server. It’s time to show you how to use it with TomatoCart system. Let’s see the beanstalkd flow again:


It’s composed of producer, job and worker. We just need to create these elements within TomatoCart. Firstly, we have to install pheanstalk client library and define namespace, class map within composer.json.

Create composer.json

  1. Create a composer.json file under the root of your TomatoCart directory.
  2. Put following content into it:
    "name": "Tomatocart/v1",
    "description": "TomatoCart opensource ecommerce system",
    "keywords": ["ecommerce"] ,
    "license": "GPL",
    "type": "project",
    "require": {
         "pda/pheanstalk": "3.1.0"
    "autoload": {
        "psr-4": {
            "Toc\\Jobs\\": "includes/jobs"

        "classmap": ["includes/classes/"]

3. Run following command to install:

composer install

Create job for sending order created email

  1. Create jobs fold under includes > classes fold.
  2. Create SendEmailJob.php within jobs fold.
  3. Put following code into SendEmailJob.php:
namespace Toc\Jobs;

use toC_Email_Template;

class SendEmailJob
    public function handle($params = [])
        $email_template = toC_Email_Template::getEmailTemplate('new_order_created');

        if (SEND_EXTRA_ORDER_EMAILS_TO != '') {
            $extra_emials = explode(',', SEND_EXTRA_ORDER_EMAILS_TO);

            if (is_array($extra_emials) && !osc_empty($extra_emials)) {
                foreach($extra_emials as $email) {
                    $email_template->addRecipient('', trim($email));


Create worker for reserving the job and perform it

  1. Create worker.php under your tomatocart root directory.
  2. Put following code into worker.php:

include 'vendor/autoload.php';

include 'includes/application_top.php';

use Pheanstalk\Pheanstalk;

$queue = new Pheanstalk('');

while (true) {
    $reserved_job = $queue->watch('tomatocart')

    $job_data = json_decode($reserved_job->getData(), false);

    $job_name = $job_data->name;
    $params = $job_data->params;

    $job_class = sprintf('Toc\Jobs\%s', $job_name);
    $job = new $job_class;


As you can see, we just connect to the local beanstalkd server and then reserve the job from tomatocart tube. Once get the job, we just need to new the job and call the handle method of it.

Putting job into beanstalkd tube – tomatocart

  1. Open includes > classes > order.php with an editor.
  2. Add following line after <?php tag:
    include DIR_FS_CATALOG . 'vendor/autoload.php';
  3. Replace sendEmail function with following code:
    function sendEmail($id) {
          $queue = new Pheanstalk\Pheanstalk('');
          $job = new stdClass();
          $job->name = 'SendEmailJob';
          $job->params = ['id' => $id];
          $job_data = json_encode($job);

In sendEmail function, we connect to local beanstalkd server and create a SendEmailJob instance and put it into tomatocart tube. In fact, sendEmail function works as a producer in beanstalkd flow.

Run worker in the background

Now, it is necessary to run the worker in the background. For simplicity, i just run the log in the while(true) block. So, it works as a damon. This is a not better way, we should use a process control system such as supervisor to monitor and control the worker. I will write an article about it in the future.

  1. Login into your server with ssh.
  2. Go to your TomatoCart root directory.
  3. Run following command to start the worker:
php worker.php &

Now, the worker will wait the job to be putted into tube and then reserve the job.

Create a new order in TomatoCart



After sending order created email asynchronously, it is really faster to complete the whole checkout procedure. Fascinating!

Final word

In this article, i will show you how to use beanstalkd with TomatoCart to send the order email asynchronously. In the third article, i will show you web interface to monitor the beanstalkd server and manage the job directly in the UI.

Looking for quality TomatoCart hosting? Check out Arvixe Web Hosting

Tags: , , , , , | Posted under TomatoCart | RSS 2.0

Author Spotlight

Jack Yin

TomatoCart Developer & Co Founder - Arvixe Web Hosting / TomatoCart Community Liaison

Leave a Reply

Your email address will not be published. Required fields are marked *