PHP code example of utopia-php / feed

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

    

utopia-php / feed example snippets


use Utopia\Feed\Batch;
use Utopia\Feed\Producer;
use Utopia\Feed\Server;
use Utopia\Feed\Store;

$store = new Store\Redis($redis, 'edge');

// Wherever things happen:
$producer = new Producer($store, source: 'urn:appwrite:cloud:fra');

$producer->produce(
    type: 'io.appwrite.edge.invalidate-rule',
    data: ['tags' => ['domain' => 'example.com']],
    subject: 'example.com',
);

// The whole feed route:
$server = new Server($store);

// GET /v1/feeds/:feedId
$batch = $server->serve($request->getParams());

$response
    ->addHeader('Content-Type', Batch::MEDIA_TYPE) // application/cloudevents-batch+json
    ->addHeader('Cache-Control', $batch->cacheControl(public: true))
    ->json($batch->toArray());

use Utopia\Client\Adapter\Curl\Client as Curl;
use Utopia\Client\Client;
use Utopia\CloudEvents\CloudEvent;
use Utopia\Feed\Consumer;
use Utopia\Feed\Cursor;
use Utopia\Feed\Exception\Transport;

$client = (new Client(new Curl()))
    ->withBaseUri('https://cloud.example.com/v1/feeds')
    ->withHeaders(['x-appwrite-jwt' => $token])
    ->withConnectionReuse();

$consumer = new Consumer($client, new Cursor\Cache($cache), name: 'cache-invalidator', feed: 'edge', timeout: 20_000);

while (true) {
    try {
        $consumer->consume(function (CloudEvent $event) use ($router) {
            $router->invalidate($event->data['tags'] ?? []);
        });
    } catch (Transport $error) {
        // Feed or cursor store unreachable. The position did not move, so
        // the next poll is already the retry — just don't spin.
        \usleep(1_000_000);
    }
}

$consumer = new Consumer($store, new Cursor\Redis($redis), name: 'audit-log');

$consumer->consume(function (CloudEvent $event) use ($mailer): bool {
    if (!$mailer->healthy()) {
        return false; // known-down dependency — same event next run
    }

    $mailer->send($event->data);

    return true;
});

$consumer = new Consumer($client, $cursor, name: 'projector', feed: 'edge', batch: 500);

$consumer->consumeChunk(function (array $events) use ($db): bool {
    try {
        $db->upsertMany(\array_map(fn (CloudEvent $event) => $event->data, $events));
    } catch (DeadlockException) {
        return false; // transient — the same chunk comes back next run
    }

    return true;
});

$consumer = new Consumer($client, $cursor, name: 'notifier', feed: 'edge', timeout: 20_000, start: Consumer::START_TIP);

try {
    $consumer->consume($handler);
} catch (\Throwable $error) {
    Console::error("[feed] blocked: {$error->getMessage()}");

    // After investigating — this event cannot and should not be handled:
    $consumer->seek($poisonEventId);
}

try {
    $consumer->consume($handler);
} catch (Transport $error) {
    if ($error->getCode() !== 404) {
        throw $error;
    }

    // Producer does not serve the feed yet; retry quietly until it does.
}
bash
composer