Laravel 5.4的Broadcast功能实践

准备工作

配置chat-demo项目

x001 克隆项目并执行一些命令

git clone https://github.com/jplhomer/laravel-realtime-chat-demo chat-demo
cd chat-demo
composer install
php artisan key:generate
npm install

laravel echo 通过npm install被安装了

x002 修改.env配置

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your db name
DB_USERNAME=your user name
DB_PASSWORD=your password

BROADCAST_DRIVER=redis
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_DRIVER=redis

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

x003 执行数据库迁移

php artisan migrate

x004 在view layout文件中引入socket.io.js

<script src="//{{ Request::getHost() }}:6001/socket.io/socket.io.js"></script></body>

{{ Request::getHost() }}:6001这部分内容更加laravel-echo-server的实际服务地址进行替换

配置laravel-echo-server

x001 下载并初始化生成laravel-echo-server.json配置文件

npm install -g laravel-echo-server
cd chat
laravel-echo-server init # 回车就OK了

x002 安装uWebSockets(macOS)

brew install openssl zlib libuv
git clone https://github.com/uNetworking/uWebSockets
cd uWebSockets
make
sudo make install

x003 修改laravel-echo-server.json配置文件,使用你自己的环境配置

{
    "authHost": "http://localhost:8000", // 如果开发环境有端口号,注意加上,默认是http://localhost
    "authEndpoint": "/broadcasting/auth",
    "clients": [
        {
            "appId": "8a7eda391f68f007",
            "key": "72b0ca933ae664949de89f3b64c2aecc"
        }
    ],
    "database": "redis",
    "databaseConfig": {
        "redis": { // 这个地方默认是一个空的,需要添加上你自己的redis的配置
            "port": 6379,
            "host": "127.0.0.1"
        },
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": null,
    "port": "6001",
    "protocol": "http",
    "socketio": { // 默认是空的,也可以运行,使用uws能获得更好的性能
         "wsEngine": "uws"
    },
    "sslCertPath": "",
    "sslKeyPath": "",
    "sslCertChainPath": "",
    "sslPassphrase": ""
}

x004 启动laravel-echo-server

laravel-echo-server start

参考资料

Laravel 的事件广播系统
Realtime chat demo using Laravel 5.4, VueJS and Pusher
laravel-echo-server

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,993评论 19 139
  • 原文链接 必备品 文档:Documentation API:API Reference 视频:Laracasts ...
    layjoy阅读 8,627评论 0 121
  • 转载 :OpenDiggawesome-github-vue 是由OpenDigg整理并维护的Vue相关开源项目库...
    果汁密码阅读 23,212评论 8 124
  • 来源:github.com Vue.js开源项目速查表:https://www.ctolib.com/cheats...
    zhangtaiwei阅读 11,684评论 1 159
  • weex ios 集成参阅:Weex学习与实践:iOS原理篇 swift集成weex 首先将weexsdk集成到项...
    John_LS阅读 3,657评论 2 11