【问题标题】:Redis not picking up Broadcast events in Laravel 5.1Redis 在 Laravel 5.1 中没有接收广播事件
【发布时间】:2015-09-10 18:25:28
【问题描述】:

我有以下事件:

<?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];
    }
}

我在 broadcasting.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"}' 那么节点应用程序确实会收到该消息。

提前干杯!

【问题讨论】:

  • 你有没有机会同时使用 redis 会话和缓存驱动程序?
  • @DavidBarker 我确实是!这会导致某种形式的冲突吗?
  • 在 5.1 发布之前我第一次使用它时,它确实发生了。我必须将缓存驱动程序设置为 redis 以外的任何内容才能使其正常工作。
  • 你确定吗,你已经在 .env 文件中配置了驱动。 broadcasting.php 有那一行 'default' => env('BROADCAST_DRIVER', 'pusher'),

标签: laravel redis socket.io laravel-5


【解决方案1】:

我自己刚才也有这个问题。

显然,广播事件使用QUEUE_DRIVER:

See "Queue Prerequisites":

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

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

【讨论】:

  • 对此我感激不尽。我将QUEUE_DRIVER 从同步切换,并且没有在我的本地计算机上运行侦听器,因此从未广播过事件。我希望我能再投票 1000 次。
猜你喜欢
  • 2015-08-29
  • 2016-06-12
  • 2023-03-04
  • 2018-03-08
  • 2017-04-28
  • 2021-05-06
  • 2023-03-24
  • 2021-08-15
  • 1970-01-01
相关资源
最近更新 更多