PHP code example of dspacelabs / queue

1. Go to this page and download the library: Download dspacelabs/queue 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/ */

    

dspacelabs / queue example snippets




use Dspacelabs\Component\Queue\Message;

// Publishing messages to a queue
$message = new Message($body);
$queue->publish($message);
/**
 * This will publish a message to the queue you created, the $body can be
 * anything you want.
 */

// Receive messages
$message = $queue->receive();
$body    = $message->getBody();
// ... Process Data ...
/**
 * $message will be the message that was published. `->receive()` can be put
 * into a foreach loop if you want to continue to process the queue until
 * all the messages are processed, use a for loop in you only want to process
 * a small number of the messages
 */

/**
 * Once you are done processing a message, it needs to be deleted from the queue
 */
$queue->delete($message);



use Dspacelabs\Component\Queue\FileQueue;
use Dspacelabs\Component\Queue\Message;

$queue = new FileQueue('queue.name', '/tmp/');
$queue->publish(new Message('Hello World!'));

// ...

$message = $queue->receive();
$body = $message->getBody(); // $body === "Hello World!"
$queue->delete($message);



use Aws\Credentials\Credentials;
use Aws\Sqs\SqsClient;
use Dspacelabs\Component\Queue\SqsQueue;

$credentials = new Credentials($accessKey, $secretKey);
$client = new SqsClient([
    'version'     => 'latest',
    'region'      => 'us-east-1',
    'credentials' => $credentials,
]);

$queue  = new SqsQueue($client, $queueUrl, $name);



// First you need to setup the Queue
$queue = new \Dspacelabs\Component\Queue\StandardQueue('queue.name');

// Create a message that will be sent to the queue
$message = new \Dspacelabs\Component\Queue\Message('Hello World A');

// Publish the message
$queue->publish($message);

// Consume all messages
/** @var Message $msg **/
while ($msg = $queue->receive()) {
    // process message
    // ...
    // Delete the Message from the queu
    $queue->delete($msg);
}



use Predis\Client;
use Dspacelabs\Component\Queue\RedisQueue;

$client = new Client();
$queue  = new RedisQueue($client, 'queue.name');



use Dspacelabs\Component\Queue\Broker;

$broker = new Broker();

// I assume you already have a queue
$broker->addQueue($queue);

// `queue.name` is the name given to the queue you created
// I assume you already have a `$message` created
$broker->get('queue.name')->publish($message);
$broker->get('queue.other')->publish($messageOther);
bash
php composer.phar