PHP code example of tina4stack / tina4php-queue

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

    

tina4stack / tina4php-queue example snippets


$config = new \Tina4\QueueConfig();
$config->queueType = 'litequeue';
$config->litequeueDatabaseName = 'queue.db';

$queue = new \Tina4\Queue($config, 'my-topic');
$producer = new \Tina4\Producer($queue);

$msg = $producer->produce('Hello World', 'user123');
echo "Produced: {$msg->messageId}\n";

foreach ($queue->consume(acknowledge: true) as $msg) {
    echo "Received: {$msg->data} from user {$msg->userId}\n";
}

$gen = $queue->consume();
$msg = $gen->current(); // Get one message, or wait for one

$queue1 = new \Tina4\Queue($config, 'orders');
$queue2 = new \Tina4\Queue($config, 'notifications');

$consumer = new \Tina4\Consumer([$queue1, $queue2], acknowledge: true, pollInterval: 0.5);

foreach ($consumer->messages() as $msg) {
    echo "From {$msg->topic}: {$msg->data}\n";
}

$consumer = new \Tina4\Consumer($queue);

$consumer->runForever(function (\Tina4\QueueMessage $msg) {
    // Process each message
    echo "Handling: {$msg->data}\n";
});

$queue = new \Tina4\Queue($config, 'bulk-jobs', batchSize: 10);

foreach ($queue->consume() as $msg) {
    // Up to 10 messages fetched per poll
    processJob($msg);
}

$config = new \Tina4\QueueConfig();
$config->queueType = 'rabbitmq';
$config->rabbitmqConfig = [
    'host' => 'localhost',
    'port' => 5672,
    'user' => 'guest',
    'password' => 'guest'
];

$config = new \Tina4\QueueConfig();
$config->queueType = 'mongo-queue';
$config->mongoQueueConfig = [
    'host' => 'localhost',
    'port' => 27017,
    'username' => 'admin',
    'password' => 'secret'
];

$config = new \Tina4\QueueConfig();
$config->queueType = 'kafka';
$config->kafkaConfig = [
    'bootstrap.servers' => 'localhost:9092'
];

$config->prefix = 'dev';
// LiteQueue table: dev_my_topic
// RabbitMQ exchange: dev_my-topic
// MongoDB collection: dev_my-topic

$producer = new \Tina4\Producer($queue, deliveryCallback: function ($backend, $err, $msg) {
    if ($err) {
        error_log("Delivery failed: " . $err->getMessage());
    }
});

try {
    $msg = $producer->produce('payload', 'user-42');
} catch (\Tina4\QueueException $e) {
    // Handle failure
}