Redis does not collect broadcast events in Laravel 5.1

I have the following event:

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

      

I am using Redis driver in broadcasting.php

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

      

Then I have a node app from the official docs that works 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);
});

      

Node app getting nothing from Redis? If I manually go to redis-cli and run `` 'PUBLISH feed' {event: "SixtyFiveContrib \ Events \ NotificationEvent"} then the node application gets this message.

Welcome in advance!

+3


source to share


1 answer


If only this problem was now.

Apparently event broadcasting uses QUEUE_DRIVER

:

See "Queue prerequisites" :



You will also need to configure and run a queue listener before broadcasting events. All event broadcasts are done using queued jobs, so your application's response time is not seriously affected.

So, to catch events right away, you can set QUEUE_DRIVER=sync

.
But this is of course not recommended as all your other tasks will sync as well. Therefore, it is best to create the correct queue handler first.

+3


source







All Articles