事件廣播
簡介
Larvel 5.1 之中新加入了事件廣播的功能,作用是把伺服器中觸發的事件通過websocket服務通知用戶端,也就是瀏覽器,用戶端js根據接受到的事件,做出相應動作。本文會用簡單的代碼展示一個事件廣播的過程。
依賴
- redis
- nodejs, socket.io
- laravel 5.1
配置
config/broadcasting.php中,如下配置'default' => env('BROADCAST_DRIVER', 'redis'),,使用redis作為php和js的通訊方式。
config/database.php中配置redis的串連。
定義一個被廣播的事件
根據Laravel文檔的說明,想讓事件被廣播,必須讓Event類實現一個Illuminate\Contracts\Broadcasting\ShouldBroadcast介面,並且實現一個方法broadcastOn。broadcastOn返回一個數組,包含了事件發送到的channel(頻道)。如下:
namespace App\Events;use App\Events\Event;use Illuminate\Queue\SerializesModels;use Illuminate\Contracts\Broadcasting\ShouldBroadcast;class SomeEvent extends Event implements ShouldBroadcast{ use SerializesModels; public $user_id; /** * Create a new event instance. * * @return void */ public function __construct($user_id) { $this->user_id = $user_id; } /** * Get the channels the event should be broadcast on. * * @return array */ public function broadcastOn() { return ['test-channel']; }}
被廣播的資料
預設情況下,Event中的所有public屬性都會被序列化後廣播。上面的例子中就是$user_id這個屬性。你也可以使用broadcastWith這個方法,明確的指出要廣播上面資料。例如:
public function broadcastWith(){ return ['user_id' => $this->user_id];}
Redis和Websocket伺服器
-
需要啟動一個Redis,事件廣播主要依賴的就是redis的sub/pub功能,具體可以看redis文檔
-
需要啟動一個websocket伺服器來和client通訊,建議使用socket.io,代碼如下:
var app = require('http').createServer(handler);var io = require('socket.io')(app);var Redis = require('ioredis');var redis = new Redis('6379', '192.168.1.106');app.listen(6001, function() { console.log('Server is running!');});function handler(req, res) { res.writeHead(200); res.end('');}io.on('connection', function(socket) { console.log('connected');});redis.psubscribe('*', function(err, count) { console.log(count);});redis.on('pmessage', function(subscribed, channel, message) { console.log(subscribed); console.log(channel); console.log(message); message = JSON.parse(message); io.emit(channel + ':' + message.event, message.data);});
這裡需要注意的是redis.on方法的定義,接收到訊息後,給client發送一個事件,事件名稱為channel + ':' + message.event。
用戶端代碼
用戶端我們也使用socket.io,作為測試,代碼盡量簡化,僅僅列印一個接受到的資料即可。如下:
var socket = io('http://localhost:6001');socket.on('connection', function (data) { console.log(data);});socket.on('test-channel:App\\Events\\SomeEvent', function(message){ console.log(message);});console.log(socket);
伺服器觸發事件
直接在router中定義個事件觸發即可。如下:
Route::get('/event', function(){ Event::fire(new \App\Events\SomeEvent(3)); return "hello world";});
測試
- 啟動redis
- 啟動websocket
- 開啟帶有用戶端代碼的頁面,可以看到websocket已經串連成功。
- 觸發事件,開啟另一個頁面 localhost/event。
這時就可以發現,第一個頁面的console中列印出了Object{user_id: 3},說明廣播成功。
我錄了一個教學視頻,大家如有不明白可以參考這個視頻。