今天,我們將在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>的默認廣播配置文件,默認情況下,請訪問多個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. 如果事件是正常事件,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<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>方法中模仿該行為。 <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中文網其他相關文章!

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

stickysessensureuserRequestSarerOutedTothesMeServerForsessionDataConsisterency.1)sessionIdentificeAssificationAssigeaSsignAssignSignSuserServerServerSustersusiseCookiesorUrlModifications.2)一致的ententRoutingDirectSsssssubsequeSssubsequeSubsequestrequestSameSameserver.3)loadBellankingDisteributesNebutesneNewuserEreNevuseRe.3)

phpoffersvarioussessionsionsavehandlers:1)文件:默認,簡單的ButMayBottLeneckonHigh-trafficsites.2)Memcached:高性能,Idealforsforspeed-Criticalapplications.3)REDIS:redis:similartomemememememcached,withddeddeddedpassistence.4)withddeddedpassistence.4)databases:gelifforcontrati forforcontrati,有用

PHP中的session是用於在服務器端保存用戶數據以在多個請求之間保持狀態的機制。具體來說,1)session通過session_start()函數啟動,並通過$_SESSION超級全局數組存儲和讀取數據;2)session數據默認存儲在服務器的臨時文件中,但可通過數據庫或內存存儲優化;3)使用session可以實現用戶登錄狀態跟踪和購物車管理等功能;4)需要注意session的安全傳輸和性能優化,以確保應用的安全性和效率。

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

絕對會話超時從會話創建時開始計時,閒置會話超時則從用戶無操作時開始計時。絕對會話超時適用於需要嚴格控制會話生命週期的場景,如金融應用;閒置會話超時適合希望用戶長時間保持會話活躍的應用,如社交媒體。

服務器會話失效可以通過以下步驟解決:1.檢查服務器配置,確保會話設置正確。 2.驗證客戶端cookies,確認瀏覽器支持並正確發送。 3.檢查會話存儲服務,如Redis,確保其正常運行。 4.審查應用代碼,確保會話邏輯正確。通過這些步驟,可以有效診斷和修復會話問題,提升用戶體驗。

session_start()iscucialinphpformanagingusersessions.1)ItInitiateSanewsessionifnoneexists,2)resumesanexistingsessions,and3)setsasesessionCookieforContinuityActinuityAccontinuityAcconActInityAcconActInityAcconAccRequests,EnablingApplicationsApplicationsLikeUseAppericationLikeUseAthenticationalticationaltication and PersersonalizedContentent。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),