PHP code example of xobotyi / beansclient

1. Go to this page and download the library: Download xobotyi/beansclient library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

xobotyi / beansclient example snippets



use xobotyi\beansclient\Beanstalkd;
use xobotyi\beansclient\Client;
use xobotyi\beansclient\Socket\SocketsSocket;

$sock   = new SocketsSocket(host: 'localhost', port: 11300, connectionTimeout: 2);
$client = new Client(socket: $sock, defaultTube: 'myAwesomeTube');

##            ##
#   PRODUCER   #
##            ##

$job = $client->put("job's payload", delay: 2);
if($job['state'] === Beanstalkd::JOB_STATE_DELAYED) {
  echo "Job {$job['id']} is ready to be reserved within 2 seconds\n";
}

##            ##
#    WORKER    #
##            ##

$client->watchTube('myAwesomeTube2');

$job = $client->reserve();

if ($job) {
    echo "Hey, i received first {$job['payload']} of job with id {$job['id']}\n";

    $client->delete($job['id']);

    echo "And i've done it!\n";
}
else {
    echo "So sad, i have nothing to do";
}

echo "Am I still connected? \n" . ($client->socket()->isConnected() ? 'Yes' : 'No') . "\n";