PHP code example of cloudfort / tez

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

    

cloudfort / tez example snippets


use Cloudfort\Tez\TezClient;

$client = new TezClient(
    endpoint: 'https://your-tez-server.example.com',
    apiKey:   'your-api-key',
    secret:   'your-base64-secret',
);

$result = $client->publish(
    channels: ['orders', 'notifications'],
    event:    'order.created',
    data:     ['id' => 42, 'total' => 99.99],
);

// $result = ['accepted' => true, 'event_id' => '...']

$client->publish(
    channels:        ['orders'],
    event:           'order.created',
    data:            ['id' => 42],
    excludeSocketId: '550e8400-e29b-41d4-a716-446655440000',
);

// Private channel
$grant = $client->createSubscriptionGrant(
    channel:  'private-orders',
    socketId: '550e8400-e29b-41d4-a716-446655440000',
);

// Presence channel
$grant = $client->createSubscriptionGrant(
    channel:  'presence-chat',
    socketId: '550e8400-e29b-41d4-a716-446655440000',
    userId:   '42',
    userInfo: ['name' => 'Jane', 'role' => 'admin'],
);

'connections' => [
    'tez' => [
        'driver'  => 'tez',
        'endpoint' => env('TEZ_ENDPOINT', 'http://localhost:8080'),
        'api_key'  => env('TEZ_KEY'),
        'secret'   => env('TEZ_SECRET'),
    ],
],

use Illuminate\Broadcasting\Channel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class OrderCreated implements ShouldBroadcast
{
    use Dispatchable, SerializesModels;

    public function __construct(public int $orderId) {}

    public function broadcastOn(): array
    {
        return [new Channel('orders')];
    }

    public function broadcastAs(): string
    {
        return 'order.created';
    }

    public function broadcastWith(): array
    {
        return ['id' => $this->orderId];
    }
}

event(new OrderCreated(42));

use Illuminate\Support\Facades\Broadcast;

Broadcast::channel('orders.{orderId}', function ($user, int $orderId) {
    return $user->canViewOrder($orderId);
});

Broadcast::channel('chat.{roomId}', function ($user, int $roomId) {
    return ['name' => $user->name, 'avatar' => $user->avatar_url];
});

use Cloudfort\Tez\Exceptions\TezException;

try {
    $client->publish(['orders'], 'order.created', ['id' => 1]);
} catch (TezException $e) {
    echo $e->getMessage();     // Human-readable error
    echo $e->statusCode;       // HTTP status code (e.g. 401, 403, 422)
    echo $e->errorCode;        // Machine-readable code (e.g. 'invalid_signature')
}
bash
php artisan vendor:publish --tag=tez-config