Over the last couple of weeks we’ve been adding Pusher to a Laravel application.

First, we set up the Client for making requests to Pusher and the Storage to store the currently active users in Setting up Pusher in a Laravel Application.

Last week we looked at authenticating users using both private and presence channels in Authenticating with Pusher in a Laravel application.

In this final instalment we will add the the functionality to subscribe and unsubscribe users from channels, as well as look at how we can push data to the client.

Listening for webhooks

When a user successfully authenticates (as we saw last week), Pusher will send us a webhook so we know we can send data to that user.

So the first thing we can do is to add a route to accept Pusher’s webhooks:

$router->post('webhooks/pusher/{webhook}', [
    'as'   => 'pusher.webhooks',
    'uses' => 'PusherController@webhooks'
]);

When we receive a request from Pusher, there will be no authentication. To prevent unauthorised requests, Pusher will include a signature that we can check against to make sure the request is valid.