使用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.