PHP code example of togomq / togomq-sdk

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

    

togomq / togomq-sdk example snippets




use TogoMQ\SDK\Config;
use TogoMQ\SDK\Client;

// Create client with defaults (only token is 



use TogoMQ\SDK\Config;

$config = (new Config('your-token-here'))
    ->withHost('custom.togomq.io')
    ->withPort(9000)
    ->withLogLevel('debug');

$client = new Client($config);



use TogoMQ\SDK\Config;
use TogoMQ\SDK\Client;
use TogoMQ\SDK\Message;

// Create client
$config = new Config('your-token');
$client = new Client($config);

// Create messages - topic is   ]),
    (new Message('orders', 'order-3'))
        ->withPostpone(60)      // Delay 60 seconds
        ->withRetention(3600),  // Keep for 1 hour
];

// Publish
$response = $client->pubBatch($messages);

echo "Published {$response->getMessagesReceived()} messages\n";

// Always close the client when done
$client->close();



use TogoMQ\SDK\Config;
use TogoMQ\SDK\Client;
use TogoMQ\SDK\SubscribeOptions;

// Create client
$config = new Config('your-token');
$client = new Client($config);

// Subscribe to specific topic
// Topic is dy()}\n";
    echo "Message UUID: {$message->getUuid()}\n";
    
    // Access variables
    $priority = $message->getVariable('priority');
    if ($priority !== null) {
        echo "Priority: {$priority}\n";
    }
}

$client->close();



// Subscribe with batch size and rate limiting
// Default values: Batch = 0 (server default 1000), SpeedPerSec = 0 (unlimited)
$options = (new SubscribeOptions('orders.*'))  // Wildcard topic
    ->withBatch(10)                             // Receive up to 10 messages at once
    ->withSpeedPerSec(100);                     // Limit to 100 messages per second

$subscription = $client->sub($options);

foreach ($subscription as $message) {
    processMessage($message);
}



// Subscribe to all topics using "*" wildcard
$options = new SubscribeOptions('*'); // "*" = all topics
$subscription = $client->sub($options);



// Subscribe to all orders topics (orders.new, orders.updated, etc.)
$options = new SubscribeOptions('orders.*');
$subscription = $client->sub($options);

// Subscribe to all topics
$options = new SubscribeOptions('*');
$subscription = $client->sub($options);



use TogoMQ\SDK\Exception\TogoMQException;

try {
    $options = new SubscribeOptions('events');
    $subscription = $client->sub($options);
    
    foreach ($subscription as $message) {
        processMessage($message);
    }
} catch (TogoMQException $e) {
    echo "Subscription error ({$e->getErrorCode()}): {$e->getMessage()}\n";
}



use TogoMQ\SDK\Message;

$message = new Message(
    topic: 'orders',           // Message topic (
    ->withVariables(['key' => 'value'])  // Custom metadata
    ->withPostpone(60)                   // Delay in seconds
    ->withRetention(3600);               // Keep for 1 hour



// When receiving messages, they      // Message topic
$uuid = $message->getUuid();                // Unique message identifier
$body = $message->getBody();                // Message payload
$variables = $message->getVariables();      // Custom metadata array
$value = $message->getVariable('key');      // Get specific variable



use TogoMQ\SDK\Exception\TogoMQException;

try {
    $response = $client->pubBatch($messages);
} catch (TogoMQException $e) {
    // Check error type
    switch ($e->getErrorCode()) {
        case TogoMQException::ERR_CODE_AUTH:
            echo "Authentication failed\n";
            break;
        case TogoMQException::ERR_CODE_CONNECTION:
            echo "Connection error\n";
            break;
        case TogoMQException::ERR_CODE_VALIDATION:
            echo "Validation error\n";
            break;
        default:
            echo "Error: {$e->getMessage()}\n";
    }
}



$config = (new Config('your-token'))
    ->withLogLevel('debug'); // debug, info, warn, error, none