PHP code example of belysh / laravel-socket-bridge

1. Go to this page and download the library: Download belysh/laravel-socket-bridge 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/ */

    

belysh / laravel-socket-bridge example snippets


namespace App\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class OrderUpdated implements ShouldBroadcast
{
    use InteractsWithSockets;

    public function __construct(public int $orderId, public string $status) {}

    public function broadcastOn(): array
    {
        return [new PrivateChannel('orders.'.$this->orderId)];
    }

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

use Illuminate\Support\Facades\Broadcast;

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

broadcast(new \App\Events\OrderUpdated($order->id, 'paid'));

use SocketBridge\Commands\CommandRegistry;

app(CommandRegistry::class)->register('order.pay', \App\SocketCommands\PayOrder::class);

use SocketBridge\Facades\Socket;

Socket::toUser($user->id)->emit('inbox.updated', ['unread' => 3]);
Socket::toRoom('private-orders.'.$order->id)
    ->emit('order.updated', ['orderId' => $order->id, 'status' => 'paid']);

use Illuminate\Support\Facades\DB;

DB::transaction(function () use ($order) {
    $order->update(['status' => 'paid']);

    Socket::durable()->toRoom('private-orders.'.$order->id)
        ->emit('order.updated', ['orderId' => $order->id, 'status' => 'paid']);
});

Socket::toRoom('private-chat.'.$chat->id)
    ->ephemeral(5)
    ->correlate($requestId)
    ->emit('chat.typing', ['active' => true]);
bash
composer socket-bridge:install
php artisan migrate
bash
php artisan socket-bridge:dev
bash
php artisan make:socket-command PayOrder