Redis没有在Laravel 5.1中接收广播事件

时间:2015-06-24 13:49:34

标签: laravel redis socket.io laravel-5

我有以下事件:

<?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驱动程序

 'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
        ],

然后我有来自官方文档的节点应用程序,它运行正常并连接到客户端:

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”}',那么节点应用确实会收到该消息。

提前干杯!

1 个答案:

答案 0 :(得分:5)

我刚才有这个问题。

显然,广播事件使用QUEUE_DRIVER

See "Queue Prerequisites"

  

在广播事件之前,您还需要配置并运行队列侦听器。所有事件广播都是通过排队的作业完成的,这样您的应用程序的响应时间就不会受到严重影响。

因此,要立即捕捉事件,您可以设置QUEUE_DRIVER=sync 但当然不建议这样做,因为你所有的其他工作也会同步进行 因此,最好先设置一个合适的队列处理程序。

相关问题