首页 >后端开发 >php教程 >Laravel广播的工作方式

Laravel广播的工作方式

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌原创
2025-03-05 09:27:14131浏览

今天,我们将在Laravel Web框架中探索广播的概念。当服务器端发生某些事情时,它允许您将通知发送到客户端。在本文中,我们将使用第三方Pusher库将通知发送到客户端。

>

如果您曾经想在Laravel中的服务器上发生某些事情时,您正在寻找广播功能的某些事情时将通知从服务器发送到客户端。现在,当用户A向用户B发送消息时,您需要实时通知用户B。 You may display a popup or an alert box that informs user B about the new message!

It's the perfect use-case to walk through the concept of broadcasting in Laravel, and that's what we'll implement in this article.

If you are wondering how the server could send notifications to the client, it's using sockets under the hood to accomplish it.让我们了解插座的基本流程,然后再深入研究实际实施。

首先,您需要一台支持Web-Sockets协议的服务器,允许客户端建立Web套接字连接。
    您可以实现自己的服务器或使用自己的服务器或使用第三方服务。我们将更喜欢本文中的后者。
  • >客户在成功连接时启动了与Web套接字服务器的Web套接字连接,并在连接成功时接收唯一的标识符。
  • >
  • 连接成功,客户将订阅某些频道,它将订阅它想要接收事件的某些频道。服务器端,当发生特定事件时,我们通过提供频道名称和事件名称来告知Web-Socket服务器。
  • ,最后,Web-Socket Server将该事件广播到该特定频道上注册的客户端。
  • >
  • >在单个go中看起来是否太多,请不要担心。当我们浏览本文时,您将掌握它。
  • 广播配置文件
  • >
  • 接下来,让我们查看
config/broadcasting.php

<?php<br><br>return [<br><br>    /*<br>    |--------------------------------------------------------------------------<br>    | Default Broadcaster<br>    |--------------------------------------------------------------------------<br>    |<br>    | This option controls the default broadcaster that will be used by the<br>    | framework when an event needs to be broadcast. You may set this to<br>    | any of the connections defined in the "connections" array below.<br>    |<br>    | Supported: "pusher", "redis", "log", "null"<br>    |<br>    */<br><br>    'default' => env('BROADCAST_DRIVER', 'null'),<br><br>    /*<br>    |--------------------------------------------------------------------------<br>    | Broadcast Connections<br>    |--------------------------------------------------------------------------<br>    |<br>    | Here you may define all of the broadcast connections that will be used<br>    | to broadcast events to other systems or over websockets. Samples of<br>    | each available type of connection are provided inside this array.<br>    |<br>    */<br><br>    'connections' => [<br><br>        'pusher' => [<br>            'driver' => 'pusher',<br>            'key' => env('PUSHER_APP_KEY'),<br>            'secret' => env('PUSHER_APP_SECRET'),<br>            'app_id' => env('PUSHER_APP_ID'),<br>            'options' => [<br>                'cluster' => env('PUSHER_APP_CLUSTER'),<br>                'useTLS' => true,<br>            ],<br>        ],<br><br>        'redis' => [<br>            'driver' => 'redis',<br>            'connection' => 'default',<br>        ],<br><br>        'log' => [<br>            'driver' => 'log',<br>        ],<br><br>        'null' => [<br>            'driver' => 'null',<br>        ],<br><br>    ],<br><br>];<br>

的默认广播配置文件,默认情况下,请访问多个core new eyan new new new nect of there norker of there corect of。使用日志适配器。当然,如果您将推动器

适配器用作我们的默认广播驱动程序。 So let's change the migration file database/migrations/XXXX_XX_XX_XXXXXX_create_messages_table.php before running the migrate command.

...<br>...<br>BROADCAST_DRIVER=pusher<br><br>PUSHER_APP_ID={YOUR_APP_ID}<br>PUSHER_APP_KEY={YOUR_APP_KEY}<br>PUSHER_APP_SECRET={YOUR_APP_SECRET}<br>PUSHER_APP_CLUSTER={YOUR_APP_CLUSTER}<br>...<br>...<br>

Now, let's run the messages table in the database.

<?php<br><br>return [<br><br>    /*<br>    |--------------------------------------------------------------------------<br>    | Default Broadcaster<br>    |--------------------------------------------------------------------------<br>    |<br>    | This option controls the default broadcaster that will be used by the<br>    | framework when an event needs to be broadcast. You may set this to<br>    | any of the connections defined in the "connections" array below.<br>    |<br>    | Supported: "pusher", "redis", "log", "null"<br>    |<br>    */<br><br>    'default' => env('BROADCAST_DRIVER', 'null'),<br><br>    /*<br>    |--------------------------------------------------------------------------<br>    | Broadcast Connections<br>    |--------------------------------------------------------------------------<br>    |<br>    | Here you may define all of the broadcast connections that will be used<br>    | to broadcast events to other systems or over websockets. Samples of<br>    | each available type of connection are provided inside this array.<br>    |<br>    */<br><br>    'connections' => [<br><br>        'pusher' => [<br>            'driver' => 'pusher',<br>            'key' => env('PUSHER_APP_KEY'),<br>            'secret' => env('PUSHER_APP_SECRET'),<br>            'app_id' => env('PUSHER_APP_ID'),<br>            'options' => [<br>                'cluster' => env('PUSHER_APP_CLUSTER'),<br>                'useTLS' => true,<br>            ],<br>        ],<br><br>        'redis' => [<br>            'driver' => 'redis',<br>            'connection' => 'default',<br>        ],<br><br>        'log' => [<br>            'driver' => 'log',<br>        ],<br><br>        'null' => [<br>            'driver' => 'null',<br>        ],<br><br>    ],<br><br>];<br>
>

创建事件类

每当您想在Laravel提出自定义事件时,都应为该事件创建类。基于事件的类型,Laravel做出了相应的反应并采取必要的操作。

如果事件是正常事件,Laravel称之为相关的侦听器类。另一方面,如果事件是广播类型的,Laravel将该事件发送到在> config/broadcasting.php

文件中配置的Web插座服务器。

当我们在示例中使用pusher服务时,Laravel将在我们的示例中使用laravel将事件发送给Pusher Server。和其他必要的推动器相关信息。

>private移动进一步,我们使用 private <code>user.{USER_ID} Echo的方法来订阅私有渠道用户。{user_id} <code>Echo。正如我们前面讨论的那样,客户必须在订阅私人渠道之前对自己进行身份验证。因此, echo<code>user.{USER_ID}对象通过使用必要的参数在后台发送XHR来执行必要的身份验证。最后,Laravel试图找到用户。{user_id}<strong>路由,它应该与我们在<ancoutes>文件中定义的路由相匹配。</ancoutes></strong>

>

>如果一切顺利,如果一切顺利,则应将Web-Socket连接与推动器Web-Socket Server和IT列表的 user.{USER_ID}

在我们的情况下,我们要聆听 newMessageNotification <antemification>事件,因此我们使用了<code> listan> listan> echo> echo <y>的方法来实现它。为了使事情变得简单,我们只会提醒我们从Pusher Server收到的消息。<p>><code>NewMessageNotification,这就是从Web-Sockockets服务器接收事件的设置。接下来,我们将在控制器文件中浏览 send<code>listen的方法,该方法提出了广播事件。Echo>

>让我们快速删除 send <code> send<p>> <code>send方法的代码。

>

send

在我们的情况下,我们将在收到新消息时通知登录用户。因此,我们试图在 send<pre class="brush:php;toolbar:false">...&lt;br&gt;...&lt;br&gt;BROADCAST_DRIVER=pusher&lt;br&gt;&lt;br&gt;PUSHER_APP_ID={YOUR_APP_ID}&lt;br&gt;PUSHER_APP_KEY={YOUR_APP_KEY}&lt;br&gt;PUSHER_APP_SECRET={YOUR_APP_SECRET}&lt;br&gt;PUSHER_APP_CLUSTER={YOUR_APP_CLUSTER}&lt;br&gt;...&lt;br&gt;...&lt;br&gt;</pre>方法中模仿该行为。<p><code>send接下来,我们使用 event <code> event

助手函数来提高 newMessageNotification <anementification>事件。由于<code> newMessAgeNotification <ante> event属于<code> shopsbroadcastNow<code>eventtype type type type type NewMessageNotificationconfig/broadcasting.phpNewMessageNotification文件中加载默认的广播配置。最后,它将 newMessAgeNotification<code>ShouldBroadcastNow事件广播到用户上的已配置的Web-Socket服务器。在我们的情况下,该事件将广播到<ancy>频道上的Pusher Web-Socket服务器。 If the ID of the recipient user is <p>, the event will be broadcast over the <code>user.{USER_ID} channel.1user.1As we discussed earlier, we already have a setup that listens to events on this channel, so it should be able to receive this event, and the alert box is displayed to the user!

How to Test Our Setup

Let's go ahead and walk through how you are supposed to test the use-case that到目前为止,我们已经构建了。

在您的浏览器中打开URL https:// your-laravel-site-domain/message/index。如果您尚未登录,您将被重定向到登录屏幕。登录后,您应该看到我们之前定义的广播视图 - 尚无幻想。当我们启用了按Pusher客户端库提供的

>设置时,它将在浏览器控制台中记录所有内容以进行调试。让我们看看当您访问http:// your-laravel-site-domain/message/index Page时,它将记录到控制台的内容。

>Pusher.logToConsole>

它已经使用Pusher Web-Socket Server打开了Web-Socket连接,并订阅了自身以聆听私人通道上的事件。当然,您可以根据登录的用户的ID具有不同的频道名称。现在,让我们在移动以测试
<?php<br><br>return [<br><br>    /*<br>    |--------------------------------------------------------------------------<br>    | Default Broadcaster<br>    |--------------------------------------------------------------------------<br>    |<br>    | This option controls the default broadcaster that will be used by the<br>    | framework when an event needs to be broadcast. You may set this to<br>    | any of the connections defined in the "connections" array below.<br>    |<br>    | Supported: "pusher", "redis", "log", "null"<br>    |<br>    */<br><br>    'default' => env('BROADCAST_DRIVER', 'null'),<br><br>    /*<br>    |--------------------------------------------------------------------------<br>    | Broadcast Connections<br>    |--------------------------------------------------------------------------<br>    |<br>    | Here you may define all of the broadcast connections that will be used<br>    | to broadcast events to other systems or over websockets. Samples of<br>    | each available type of connection are provided inside this array.<br>    |<br>    */<br><br>    'connections' => [<br><br>        'pusher' => [<br>            'driver' => 'pusher',<br>            'key' => env('PUSHER_APP_KEY'),<br>            'secret' => env('PUSHER_APP_SECRET'),<br>            'app_id' => env('PUSHER_APP_ID'),<br>            'options' => [<br>                'cluster' => env('PUSHER_APP_CLUSTER'),<br>                'useTLS' => true,<br>            ],<br>        ],<br><br>        'redis' => [<br>            'driver' => 'redis',<br>            'connection' => 'default',<br>        ],<br><br>        'log' => [<br>            'driver' => 'log',<br>        ],<br><br>        'null' => [<br>            'driver' => 'null',<br>        ],<br><br>    ],<br><br>];<br>
>方法的过程中保持此页面打开。

send接下来,让我们打开http:// your-laravel-site-domain/message/message/message/message/send url在另一个选项卡或其他浏览器中发送url。如果您要使用其他浏览器,则需要登录才能访问该页面。发生了。

,它告诉您,您刚刚从

>频道上的Pusher Web-Socket Server收到了

事件。转到您的推动器帐户并导航到您的应用程序。在

debug
...<br>...<br>BROADCAST_DRIVER=pusher<br><br>PUSHER_APP_ID={YOUR_APP_ID}<br>PUSHER_APP_KEY={YOUR_APP_KEY}<br>PUSHER_APP_SECRET={YOUR_APP_SECRET}<br>PUSHER_APP_CLUSTER={YOUR_APP_CLUSTER}<br>...<br>...<br>

> consoleAppEventsNewMessageNotification>下,您应该能够查看已记录的消息。private-user.2

> ,这将我们带到了本文的结尾!希望在我试图最大程度地简化事物时,这并不是太多了。>

>结论

今天,我们经历了Laravel -Broadcasting的最少讨论的功能之一。它允许您使用Web插座发送实时通知。在本文的整个过程中,我们建立了一个现实世界的示例,该示例证明了上述概念。

以上是Laravel广播的工作方式的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn