Broadcasting
Introduction
In many modern web applications, WebSockets are used to implement realtime, live-updating user interfaces. When some data is updated on the server, a message is typically sent over a WebSocket connection to be handled by the client. WebSockets provide a more efficient alternative to continually polling your application's server for data changes that should be reflected in your UI.
For example, imagine your application is able to export a user's data to a CSV file and email it to them. However, creating this CSV file takes several minutes so you choose to create and mail the CSV within a queued job. When the CSV has been created and mailed to the user, we can use event broadcasting to dispatch a App\Events\UserDataExported
event that is received by our application's JavaScript. Once the event is received, we can display a message to the user that their CSV has been emailed to them without them ever needing to refresh the page.
To assist you in building these types of features, Laravel makes it easy to "broadcast" your server-side Laravel events over a WebSocket connection. Broadcasting your Laravel events allows you to share the same event names and data between your server-side Laravel application and your client-side JavaScript application.
The core concepts behind broadcasting are simple: clients connect to named channels on the frontend, while your Laravel application broadcasts events to these channels on the backend. These events can contain any additional data you wish to make available to the frontend.
Supported Drivers
By default, Laravel includes two server-side broadcasting drivers for you to choose from: Pusher Channels and Ably. However, community driven packages such as laravel-websockets and soketi provide additional broadcasting drivers that do not require commercial broadcasting providers.
Before diving into event broadcasting, make sure you have read Laravel's documentation on events and listeners.
Server Side Installation
To get started using Laravel's event broadcasting, we need to do some configuration within the Laravel application as well as install a few packages.
Event broadcasting is accomplished by a server-side broadcasting driver that broadcasts your Laravel events so that Laravel Echo (a JavaScript library) can receive them within the browser client. Don't worry - we'll walk through each part of the installation process step-by-step.
Configuration
All of your application's event broadcasting configuration is stored in the config/broadcasting.php
configuration file. Laravel supports several broadcast drivers out of the box: Pusher Channels, Redis, and a log
driver for local development and debugging. Additionally, a null
driver is included which allows you to totally disable broadcasting during testing. A configuration example is included for each of these drivers in the config/broadcasting.php
configuration file.
Broadcast Service Provider
Before broadcasting any events, you will first need to register the App\Providers\BroadcastServiceProvider
. In new Laravel applications, you only need to uncomment this provider in the providers
array of your config/app.php
configuration file. This BroadcastServiceProvider
contains the code necessary to register the broadcast authorization routes and callbacks.
Queue Configuration
You will also need to configure and run a queue worker. All event broadcasting is done via queued jobs so that the response time of your application is not seriously affected by events being broadcast.
Pusher Channels
If you plan to broadcast your events using Pusher Channels, you should install the Pusher Channels PHP SDK using the Composer package manager:
composer require pusher/pusher-php-server
Next, you should configure your Pusher Channels credentials in the config/broadcasting.php
configuration file. An example Pusher Channels configuration is already included in this file, allowing you to quickly specify your key, secret, and application ID. Typically, these values should be set via the PUSHER_APP_KEY
, PUSHER_APP_SECRET
, and PUSHER_APP_ID
environment variables:
PUSHER_APP_ID=your-pusher-app-id
PUSHER_APP_KEY=your-pusher-key
PUSHER_APP_SECRET=your-pusher-secret
PUSHER_APP_CLUSTER=mt1
The config/broadcasting.php
file's pusher
configuration also allows you to specify additional options
that are supported by Channels, such as the cluster.
Next, you will need to change your broadcast driver to pusher
in your .env
file:
BROADCAST_DRIVER=pusher
Finally, you are ready to install and configure Laravel Echo, which will receive the broadcast events on the client-side.
Open Source Pusher Alternatives
The laravel-websockets and soketi packages provide Pusher compatible WebSocket servers for Laravel. These packages allow you to leverage the full power of Laravel broadcasting without a commercial WebSocket provider. For more information on installing and using these packages, please consult our documentation on open source alternatives.
Ably
If you plan to broadcast your events using Ably, you should install the Ably PHP SDK using the Composer package manager:
composer require ably/ably-php
Next, you should configure your Ably credentials in the config/broadcasting.php
configuration file. An example Ably configuration is already included in this file, allowing you to quickly specify your key. Typically, this value should be set via the ABLY_KEY
environment variable:
ABLY_KEY=your-ably-key
Next, you will need to change your broadcast driver to ably
in your .env
file:
BROADCAST_DRIVER=ably
Finally, you are ready to install and configure Laravel Echo, which will receive the broadcast events on the client-side.
Open Source Alternatives
PHP
The laravel-websockets package is a pure PHP, Pusher compatible WebSocket package for Laravel. This package allows you to leverage the full power of Laravel broadcasting without a commercial WebSocket provider. For more information on installing and using this package, please consult its official documentation.
Node
Soketi is a Node based, Pusher compatible WebSocket server for Laravel. Under the hood, Soketi utilizes µWebSockets.js for extreme scalability and speed. This package allows you to leverage the full power of Laravel broadcasting without a commercial WebSocket provider. For more information on installing and using this package, please consult its official documentation.
Client Side Installation
Pusher Channels
Laravel Echo is a JavaScript library that makes it painless to subscribe to channels and listen for events broadcast by your server-side broadcasting driver. You may install Echo via the NPM package manager. In this example, we will also install the pusher-js
package since we will be using the Pusher Channels broadcaster:
npm install --save-dev laravel-echo pusher-js
Once Echo is installed, you are ready to create a fresh Echo instance in your application's JavaScript. A great place to do this is at the bottom of the resources/js/bootstrap.js
file that is included with the Laravel framework. By default, an example Echo configuration is already included in this file - you simply need to uncomment it:
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
forceTLS: true
});
Once you have uncommented and adjusted the Echo configuration according to your needs, you may compile your application's assets:
npm run dev
To learn more about compiling your application's JavaScript assets, please consult the documentation on Laravel Mix.