By using Laravel 5.1 you already have the Pusher library integrated into your application so there's no installation required. We've already done all the set up we need when we set our environment variables.
config/broadcasting.php
. Luckily it's the default broadcaster so we don't actually need to configure anything.
Laravel 5.1 comes with an in-built Pusher Event Broadcaster that we're going to use. To use this we need to follow a few conventions.
Firstly, we need to define a class that represents our event. For simplicity we'll add this directly to app/Http/routes.php
for now.
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class TestEvent implements ShouldBroadcast
{
public $text;
public function __construct($text)
{
$this->text = $text;
}
public function broadcastOn()
{
return ['test-channel'];
}
}
As with the previous example, we'll go in to the details of channels and events in a later exercise.
Next, update the contents of the app/Http/routes.php
file with the following:
get('/broadcast', function() {
event(new TestEvent('Broadcasting in Laravel using Pusher!'));
return view('welcome');
});
As before, to test:
php artisan serve
)/broadcast
route in the Laravel app (or simply refresh your tab), http://localhost:8000/broadcastAgain, you'll see the event appear in the Pusher Debug Console. It's working!
When things don't work as expected it's really useful to be able to debug your server-side integration with Pusher.