How to use Laravel's broadcasting with ReactJS for real-time updates



Image not found!!

To use Laravel's broadcasting feature with ReactJS for real-time updates, you can follow these general steps. Laravel provides broadcasting through Laravel Echo and Pusher by default, but you can also use other broadcasting drivers.

Here's a step-by-step guide:

1. Set up Laravel Broadcasting

  1. Install Laravel Echo and Pusher:

    bash
    composer require pusher/pusher-php-server npm install --save laravel-echo pusher-js
  2. Configure .env: Add your Pusher credentials to your .env file:

    env
    BROADCAST_DRIVER=pusher PUSHER_APP_ID=your-app-id PUSHER_APP_KEY=your-app-key PUSHER_APP_SECRET=your-app-secret PUSHER_APP_CLUSTER=your-app-cluster
  3. Configure broadcasting.php: Set up your broadcasting configuration in config/broadcasting.php:

    php
    'connections' => [ 'pusher' => [ 'driver' => 'pusher', 'key' => env('PUSHER_APP_KEY'), 'secret' => env('PUSHER_APP_SECRET'), 'app_id' => env('PUSHER_APP_ID'), 'options' => [ 'cluster' => env('PUSHER_APP_CLUSTER'), 'encrypted' => true, ], ], ],
  4. Set up Events: Create an event class that extends Illuminate\Contracts\Broadcasting\ShouldBroadcast for the data you want to broadcast. Example:

    php
    // app/Events/UpdateEvent.php use Illuminate\Broadcasting\Channel; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Broadcasting\PresenceChannel; use Illuminate\Broadcasting\PrivateChannel; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; class UpdateEvent implements ShouldBroadcast { public $data; public function __construct($data) { $this->data = $data; } public function broadcastOn() { return new Channel('updates'); } }
  5. Broadcast the Event: In your controller or wherever you want to broadcast the event, use:

    php
    broadcast(new UpdateEvent($yourData));

2. Set up Laravel Echo in ReactJS

  1. Install Laravel Echo and Pusher in ReactJS:

    bash
    npm install --save laravel-echo pusher-js
  2. Configure Laravel Echo: Set up Laravel Echo in your ReactJS application. For example, in your main JavaScript file:

    jsx
    import Echo from 'laravel-echo'; window.Echo = new Echo({ broadcaster: 'pusher', key: process.env.REACT_APP_PUSHER_APP_KEY, cluster: process.env.REACT_APP_PUSHER_APP_CLUSTER, encrypted: true, });
  3. Listen for Events: Wherever you want to listen for events (e.g., a React component), you can use the following:

    jsx
    useEffect(() => { window.Echo.channel('updates').listen('UpdateEvent', (event) => { console.log('Event received:', event); // Update your React component with the received data }); return () => { // Clean up event listeners when the component unmounts window.Echo.leaveChannel('updates'); }; }, []);

3. Start Broadcasting

Make sure you have your Laravel development server running and your ReactJS application up. Laravel Echo will handle the connection between the two.

When you trigger an event in Laravel, the ReactJS component should receive the event and update accordingly.

Note: This example uses Pusher as the broadcasting driver. If you use a different driver, adjust the configurations accordingly.