准备工作
- 基于Realtime chat demo using Laravel 5.4, VueJS and Pusher该项目库, 这里我们给这个项目起个名字叫==chat-demo==
- laravel-echo
- laravel-echo-server
配置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