PHP code example of behzadsh / rabbitmq-broadcaster

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

    

behzadsh / rabbitmq-broadcaster example snippets




return [

    // ...

    'connections' => [

        // other connections config
        
        'rabbitmq' => [
            'driver' => 'rabbitmq',
            'connection' => [
                'host' => env('RABBITMQ_HOST', 'localhost'),
                'port' => env('RABBITMQ_PORT', 5672),
                'user' => env('RABBITMQ_USER', 'guest'),
                'password' => env('RABBITMQ_PASSWORD', 'guest'),
            ],
            'default_exchange_type' => 'fanout', // It can be `fanout` or `topic`
            'exchange_configs' => [ // optional
                'exchange1' => [
                    'type' => 'fanout',
                    'passive' => false,
                    'durable' => true,
                    'auto_delete' => false,
                    'internal' => false,
                ],
                'exchange2' => [
                    'type' => 'topic',
                    'passive' => false,
                    'durable' => false,
                    'auto_delete' => true,
                    'internal' => false,
                ],
            ], 
        ],

    ],

];



return [

    // ...
    
    'providers' => [
        // Other service providers...
        Behzadsh\RabbitMQBroadcaster\RabbitMQBroadcasterServiceProvider::class,
    ]
    
    // ...
    
];



namespace App\Events;
 
use Illuminate\Broadcasting\Channel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
 
class UserRegistered implements ShouldBroadcast
{
    /**
     * Create a new event instance.
     */
    public function __construct(
        public int $userId,
        public string $name,
        public string $email,
    ) {}
 
    /**
     * Get the channels the event should broadcast on.
     *
     * @return array<int, \Illuminate\Broadcasting\Channel>
     */
    public function broadcastOn(): array
    {
        return [
            new Channel('user.events'),
        ];
    }
}



namespace App\Events;
 
use Illuminate\Broadcasting\Channel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;
 
class ServerCreated implements ShouldBroadcast
{
    use SerializesModels;
 
    /**
     * Create a new event instance.
     */
    public function __construct(
        public User $user,
    ) {}
 
    /**
     * Get the channels the event should broadcast on.
     *
     * @return array<int, \Illuminate\Broadcasting\Channel>
     */
    public function broadcastOn(): array
    {
        return [
            new Channel('user.events'),
        ];
    }
    
    /**
     * Get the data to broadcast.
     *
     * @return array<string, mixed>
     */
    public function broadcastWith(): array
    {
        return [
            'userId' => $this->user->id,
            'name' => $this->user->name,
            'email' => $this->user->email,
        ];
    }
}