Redis未在Laravel 5.1中接收广播事件 [英] Redis not picking up Broadcast events in Laravel 5.1
问题描述
我有以下事件:
<?php
namespace SixtyFiveContrib\Events;
use Auth;
use SixtyFiveContrib\Events\Event;
use SixtyFiveContrib\Models\Notification;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
/**
* NotificationEvent
*
*/
class NotificationEvent extends Event implements ShouldBroadcast
{
use SerializesModels;
public $notification;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Notification $notification)
{
$this->notification = $notification;
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return ['feed', 'updates'];
}
public function broadcastWith()
{
return ['notification' => $this->notification];
}
}
我在broadcast.php中使用Redis驱动程序
I'm using the Redis driver in broadcasting.php
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
然后我有来自官方文档的节点应用程序,该应用程序运行良好并且可以连接到客户端:
Then I have the node app from the official docs, which runs fine and connects to the client:
var app = require('http').createServer(handler);
var io = require('socket.io')(app);
var Redis = require('ioredis');
var redis = new Redis();
app.listen(3000, function() {
console.log('Server is running!');
});
function handler(req, res) {
res.writeHead(200);
res.end('Ayup.');
}
io.on('connection', function(socket) {
//
});
redis.psubscribe('*', function(err, count) {
console.log(err);
});
redis.on('pmessage', function(subscribed, channel, message) {
message = JSON.parse(message);
console.log(subscribed);
console.log(channel);
console.log(message);
io.emit(channel + ':' + message.event, message.data);
});
节点应用程序没有从Redis接收任何信息吗?如果我手动进入redis-cli并运行```PUBLISH feed'{event:"SixtyFiveContrib \ Events \ NotificationEvent"}',则节点应用程序确实会收到该消息.
The node app isn't receiving anything from Redis? If I manually go into redis-cli and run ``` PUBLISH feed '{event: "SixtyFiveContrib\Events\NotificationEvent"}' then the node app does receive that message.
提前加油!
推荐答案
我自己刚刚遇到了这个问题.
Had this problem myself just now.
显然,广播的事件使用QUEUE_DRIVER
:
在广播事件之前,您还需要配置并运行队列侦听器.所有事件广播都是通过排队的作业完成的,因此不会严重影响应用程序的响应时间.
Before broadcasting events, you will also need to configure and run a queue listener. All event broadcasting is done via queued jobs so that the response time of your application is not seriously affected.
因此,要立即捕获事件,可以设置QUEUE_DRIVER=sync
.
但这当然是不建议的,因为您的所有其他作业也将同步运行.
因此最好先设置适当的队列处理程序.
So, to catch the events immediately, you could set QUEUE_DRIVER=sync
.
But this is of course not advised, since all your other jobs would run in sync as well.
So it's better to set up a proper queue handler first.
这篇关于Redis未在Laravel 5.1中接收广播事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!