网页
综合
更新时间 2026-11-13 17:33:11
Broadcasting | Laravel 13.x - The clean stack for Artisans and agents
自动蜘蛛
laravel.com
正文约 6000 字
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 an 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.
By default, Laravel includes four server-side broadcasting drivers for you to choose from: Laravel Reverb, Pusher Channels, Ably, and Mercure.
Before diving into event broadcasting, make sure you have read Laravel's documentation on events and listeners.
By default, broadcasting is not enabled in new Laravel applications. You may enable broadcasting using the install:broadcasting Artisan command:
The install:broadcasting command will prompt you for which event broadcasting service you would like to use. In addition, it will create the config/broadcasting.php configuration file and the routes/channels.php file where you may register your application's broadcast authorization routes and callbacks.
Laravel supports several broadcast drivers out of the box: Laravel Reverb, Pusher Channels, Ably, Mercure, and a log driver for local development and debugging. Additionally, a null driver is included which allows you to disable broadcasting during testing. A configuration example is included for each of these drivers in the config/broadcasting.php configuration file.
All of your application's event broadcasting configuration is stored in the config/broadcasting.php configuration file. Don't worry if this file does not exist in your application; it will be created when you run the install:broadcasting Artisan command.
Once you have enabled event broadcasting, you're ready to learn more about defining broadcast events and listening for events. If you're using Laravel's React, Vue, or Svelte starter kits, you may listen for events using Echo's useEcho hook.
Before broadcasting any events, you should first 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. The Mercure driver is an exception, as it broadcasts events synchronously by default.
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.
To quickly enable support for Laravel's broadcasting features while using Reverb as your event broadcaster, invoke the install:broadcasting Artisan command with the --reverb option. This Artisan command will install Reverb's required Composer and NPM packages and update your application's .env file with the appropriate variables:
When running the install:broadcasting command, you will be prompted to install Laravel Reverb. Of course, you may also install Reverb manually using the Composer package manager:
Once the package is installed, you may run Reverb's installation command to publish the configuration, add Reverb's required environment variables, and enable event broadcasting in your application:
You can find detailed Reverb installation and usage instructions in the Reverb documentation.
To quickly enable support for Laravel's broadcasting features while using Pusher as your event broadcaster, invoke the install:broadcasting Artisan command with the --pusher option. This Artisan command will prompt you for your Pusher credentials, install the Pusher PHP and JavaScript SDKs, and update your application's .env file with the appropriate variables:
To install Pusher support manually, you should install the Pusher Channels PHP SDK using the Composer package manager:
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, you should configure your Pusher Channels credentials in your application's .env file:
The config/broadcasting.php file's pusher configuration also allows you to specify additional options that are supported by Channels, such as the cluster.
Then, set the BROADCAST_CONNECTION environment variable to pusher in your application's .env file:
Finally, you are ready to install and configure Laravel Echo, which will receive the broadcast events on the client-side.
If you plan to use end-to-end encrypted private channels, you should add an encryption_master_key_base64 option containing a bas
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。