PHP code example of accolon / kara

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

    

accolon / kara example snippets


// TestProducer.php
use Kara\Producer;
use Kara\Serialize;

class TestProducer extends Producer
{
    protected string $topic = "test";
    // Types: JSON, PHP, TEXT
    protected int $type = Serialize::JSON;
}

$producer = new TestProducer();
$producer->send([
    "message" => "Hello!"
]);

// TestConsumer.php
use Kara\Consumer;
use Kara\Message;

class TestConsumer extends Consumer
{
    protected string $topic = "test";

    public function handle(Message $message)
    {
        echo "Topic: {$this->topic} -> " . $message->payload . "\n";
    }
}


// index.php
use Kara\ManagerConsumer;

$manager = new ManagerConsumer();

$manager->addConsumer(new TestConsumer());

$manager->run();
bash
php index.php