Introduction
Real-time updates have become essential in modern applications. Laravel Broadcasting with Pusher makes this incredibly easy.
Setup
composer require pusher/pusher-php-server
In your .env file:
BROADCAST_DRIVER=pusher
PUSHER_APP_ID=your_id
PUSHER_APP_KEY=your_key
PUSHER_APP_SECRET=your_secret
PUSHER_APP_CLUSTER=mt1
Creating an Event
php artisan make:event OrderStatusUpdated
In the event class:
class OrderStatusUpdated implements ShouldBroadcast
{
public function broadcastOn() {
return new PrivateChannel('orders.'.$this->order->id);
}
}
On Flutter
final pusher = PusherChannelsFlutter.getInstance();
await pusher.init(apiKey: 'key', cluster: 'mt1');
await pusher.connect();