Laravel廣播的工作方式
今天,我們將在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中看起來是否太多,請不要擔心。當我們瀏覽本文時,您將掌握它。
- 廣播配置文件
- > 接下來,讓我們查看
。
<?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>
適配器用作我們的默認廣播驅動程序。 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. 如果事件是正常事件,Laravel稱之為相關的偵聽器類。另一方面,如果事件是廣播類型的,Laravel將該事件發送到在> config/broadcasting.php > >如果一切順利,如果一切順利,則應將Web-Socket連接與推動器Web-Socket Server和IT列表的 user.{USER_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>
創建事件類
每當您想在Laravel提出自定義事件時,都應為該事件創建類。基於事件的類型,Laravel做出了相應的反應並採取必要的操作。 private
移動進一步,我們使用 private <code>user.{USER_ID}
Echo的方法來訂閱私有渠道用戶。 {user_id} <code>Echo
。正如我們前面討論的那樣,客戶必須在訂閱私人渠道之前對自己進行身份驗證。因此, echo<code>user.{USER_ID}
對象通過使用必要的參數在後台發送XHR來執行必要的身份驗證。最後,Laravel試圖找到用戶。 {user_id}<strong>路由,它應該與我們在<ancoutes>文件中定義的路由相匹配。 </ancoutes></strong>
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<div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">...<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></pre><div class="contentsignin">登入後複製</div></div><div class="contentsignin">登入後複製</div></div><div class="contentsignin">登入後複製</div></div>方法中模仿該行為。 <p><code>send
接下來,我們使用 event <code> event
助手函數來提高 newMessageNotification <anementification>事件。由於<code> newMessAgeNotification <ante> event屬於<code> shopsbroadcastNow<code>event
type type type type type NewMessageNotification
文件中加載默認的廣播配置。最後,它將 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.1
user.1
As 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
>
<?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中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

PHP8.1中的枚舉功能通過定義命名常量增強了代碼的清晰度和類型安全性。 1)枚舉可以是整數、字符串或對象,提高了代碼可讀性和類型安全性。 2)枚舉基於類,支持面向對象特性,如遍歷和反射。 3)枚舉可用於比較和賦值,確保類型安全。 4)枚舉支持添加方法,實現複雜邏輯。 5)嚴格類型檢查和錯誤處理可避免常見錯誤。 6)枚舉減少魔法值,提升可維護性,但需注意性能優化。

SOLID原則在PHP開發中的應用包括:1.單一職責原則(SRP):每個類只負責一個功能。 2.開閉原則(OCP):通過擴展而非修改實現變化。 3.里氏替換原則(LSP):子類可替換基類而不影響程序正確性。 4.接口隔離原則(ISP):使用細粒度接口避免依賴不使用的方法。 5.依賴倒置原則(DIP):高低層次模塊都依賴於抽象,通過依賴注入實現。

會話劫持可以通過以下步驟實現:1.獲取會話ID,2.使用會話ID,3.保持會話活躍。在PHP中防範會話劫持的方法包括:1.使用session_regenerate_id()函數重新生成會話ID,2.通過數據庫存儲會話數據,3.確保所有會話數據通過HTTPS傳輸。

靜態綁定(static::)在PHP中實現晚期靜態綁定(LSB),允許在靜態上下文中引用調用類而非定義類。 1)解析過程在運行時進行,2)在繼承關係中向上查找調用類,3)可能帶來性能開銷。

RESTAPI設計原則包括資源定義、URI設計、HTTP方法使用、狀態碼使用、版本控制和HATEOAS。 1.資源應使用名詞表示並保持層次結構。 2.HTTP方法應符合其語義,如GET用於獲取資源。 3.狀態碼應正確使用,如404表示資源不存在。 4.版本控制可通過URI或頭部實現。 5.HATEOAS通過響應中的鏈接引導客戶端操作。

在PHP中,異常處理通過try,catch,finally,和throw關鍵字實現。 1)try塊包圍可能拋出異常的代碼;2)catch塊處理異常;3)finally塊確保代碼始終執行;4)throw用於手動拋出異常。這些機制幫助提升代碼的健壯性和可維護性。

匿名類在PHP中的主要作用是創建一次性使用的對象。 1.匿名類允許在代碼中直接定義沒有名字的類,適用於臨時需求。 2.它們可以繼承類或實現接口,增加靈活性。 3.使用時需注意性能和代碼可讀性,避免重複定義相同的匿名類。
