使用Laravel自託管您自己的Websocket-簡單,快速



您是否曾經想過使用Websockets而不使用Pusher之類的外部服務?在本視頻中,我將向您展示如何最終在沒有任何外部服務的情況下利用Laravel中的websockets功能。我們將使用Laravel Echo來處理前端的套接字,並使用Laravel事件來觸發伺服器端的事件。

今天我們將用來操作Websocket的方法是一個稱為laravel-websockets的軟體包。該軟體包實際上將一個低級Web套接字伺服器Ratchet的實例導入到我們的Laravel項目中,從而使我們能夠將Websocket伺服器作為現有Laravel應用程序的一部分進行託管。這很方便,因為所有內容都是在一個Laravel應用程序中包含的。

您可以在需要Web套接字的Laravel應用程序中運行此Web套接字伺服器,也可以選擇託管一個獨立的應用程序,該應用程序為您自己的自託管Websocket平台運行此程序包。就像其他流行的websocket服務一樣,它可以支持多個客戶端。

這個軟體包擴展性很好,僅使用一個簡單的512Mb ram VPS伺服器就可以維護超過15,000個並發連接,該伺服器通常每月花費約5美元。

▼▼▼鏈接▼▼▼

下一步(可選):配置Laravel-Websockets-了解設置:https://youtu.be/V4Nqog5eXuQ

在YouTube上的「我的完整(免費)」課程中詳細了解Websocket:https://www.youtube.com/playlist?list = PLwAKR305CRO9rlj-U9oOi4m2sQaWN6XA8

Laravel-Websockets軟體包:https://github.com/beyondcode/laravel-websockets

Laravel-Websockets文檔:https://docs.beyondco.de/laravel-websockets/

安裝片段:https://docs.beyondco.de/laravel-websockets/1.0/getting-started/installation.html

該軟體包可擴展嗎?:https://docs.beyondco.de/laravel-websockets/1.0/faq/scaling.html

▼▼▼代碼片段▼▼▼

這是一個簡單的設置教程,因此沒有提供GitHub存儲庫。

在現有的Laravel應用中安裝
1)通過作曲家安裝:
作曲家需要beyondcode / laravel-websockets

2)發布遷移文件:
php artisan vendor:publish –provider =「 BeyondCode LaravelWebSockets WebSocketsServiceProvider」 –tag =「 migrations」

3)發布配置文件:
php artisan供應商:發布–provider =「 BeyondCode LaravelWebSockets WebSocketsServiceProvider」 –tag =「 config」

4)運行遷移:
PHP的工匠遷移

要啟動Web套接字伺服器,請運行:
PHP的工匠websockets:服務

====跟我來====

訂閱新版本!

訂閱DevMarketer Insider
https://confirmsubscription.com/h/d/5EDC91CF554832D1

Twitter-http://twitter.com/_jacurtis
(問我問題!)

====問題? ====

在下面發表評論,我或其他人可以為您提供幫助。
對於快速問題,您可能還想在Twitter上問我,我幾乎立即做出回應。

給我發電子郵件[email protected]

感謝您的支持! 。

26 comments
  1. wow i can't thank you enough!!
    but i have a question…
    i have a website (not a laravel app) and it connects to laravel eloquent to fetch data and insert using ajax
    how can i subscribe to laravel websockets channel and listen to it using js?
    thank you so much man

  2. Could you please help me with running this websocket on linux live server. I have put my all efforts into this but nothing came useful. Plese make a seperate video for running queue and websocket using supervisor. It will be really appreciated.

  3. Hi, did everything as in tutorial, set up in .env APP_URL=http://websockets.test , then artisan phph serve , php artisan websockets:serve – but in browser on address http://websockets.test I get only 'page can't be found' . Is it something missing? Do I do something wrong?

  4. Can we have a tutorial that uses redis as the driver for broadcast. Also i had to face difficulties inplementing your approach on laravel 5.8 – 6.x

  5. Hey DevMarketer! Great video, just one question:

    Can I use this to couple with a separate Nuxt application on a different domain? Can't seem to find anything about it?

    I have my backend Laravel API working fine with this, just would also like to post to my front-end Nuxt application as well…

  6. Hey ended up with this error any help would be great. "Argument 2 passed to BeyondCodeLaravelWebSocketsAppsApp::__construct() must be of the type string, null given

  7. 2 questions:
    -How to set this up with let's encrypt? I cannot get it to work at all.
    -If let's encrypt changes the SSL every 30 days, how to keep the paths working?

Comments are closed.