Laravel 廣播系統例子

cocoyo發表於2018-05-18

這裡使用了私有頻道Presence 頻道做了不同的例子:

Presence 頻道當使用者加入或傳送訊息該頻道時,通知其它加入該頻道的使用者:

file

file

私有頻道我這裡定義了一個artisan命令傳送訊息給指定的使用者:

php artisan command:push-message {user} {message}

第一個引數是傳送哪個使用者,第二個引數是需要傳送的訊息,如圖:

file

可以幫助大家在看廣播系統時結合例子起來看更深刻:relaxed:

下載專案

git clone git@github.com:ningge123/laravel-echo-demo.git

執行:

composer install 

下載前端資源

npm install

配置資料庫:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=pusher
DB_USERNAME=homestead
DB_PASSWORD=secret

配置redis:

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

然後還需安裝laravel-echo-server

npm install -g laravel-echo-server  # 這裡是全域性安裝

初始化服務端

$ laravel-echo-server init 
? Do you want to run this server in development mode? Yes
? Which port would you like to serve from? 6001
? Which database would you like to use to store presence channel members? redis
? Enter the host of your Laravel authentication server. archerwong.cn
? Will you be serving on http or https? http
? Do you want to generate a client ID/Key for HTTP API? Yes
? Do you want to setup cross domain access to the API? No
appId: c953434932b06864
key: 551440289d2d41c81e87d55c1d0217e5
Configuration file saved. Run laravel-echo-server start to run server.

執行服務端

$ laravel-echo-server start

L A R A V E L  E C H O  S E R V E R

version 1.3.6

⚠ Starting server in DEV mode...

✔  Running at localhost on port 6001
✔  Channels are ready.
✔  Listening for http events...
✔  Listening for redis events...

Server ready!

最後附上GitHub地址:https://github.com/ningge123/laravel-echo-...

備註

之前使用的是pusher發現太慢了,然後改成redis

感謝!:laughing:

本作品採用《CC 協議》,轉載必須註明作者和本文連結

如此.

相關文章