Framework: Redis not picking up Broadcast events

Created on 24 Jun 2015  Â·  21Comments  Â·  Source: laravel/framework

Hi folks,

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 
 *
 * @author    Ewan Valentine <[email protected]>
 * @copyright 65Twenty 2015
 */
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'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);
});

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.

Cheers in advance!

Most helpful comment

I want to give an update in case this helps someone else... In my case it was user error (Me). When you're broadcasting if your Event "implements ShouldBroadcast" by default that is using the queue driver. So if your queue driver is sync.. then of course executes immediately. If you change it to "redis" well then you better have a listener processing those jobs.. (LOL). In my case I was broadcasting and had a socket.io listener in JS on client side.. (Hopefully that makes sense).

You can in fact use redis for queue and broadcast just fine.. just take note of that ShouldBroadcast vs ShouldBroadcastNow.

All 21 comments

I'm using Redis for caching and sessions also. Someone on Stackoverflow mentioned that could be the issue?

That was me on SO. I had this same issue with an almost identical setup above. Changing the cache driver to file resolved the problem.

Ahaaa! Thanks! I'll try switching those out to Memcache in the interim, thanks for the advice. I'll leave this open for the Laravel maintainers benefit

It's a good idea to use separate redis databases. You can configure them in the database.php file.

Okay great I'll take a look, thanks!

This didn't work, is there anything I need to do in terms of configuring Redis?

database.php

    'redis' => [

        'cluster' => false,

        'default' => [
            'host'     => '127.0.0.1',
            'port'     => 6379,
            'database' => 0,
        ],
        'pubsub' => [
            'host'     => '127.0.0.1',
            'port'     => 6379,
            'database' => 1,
        ],
    ],

broadcast.php

    'connections' => [
        'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_KEY'),
            'secret' => env('PUSHER_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
        ],
        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
        ],
        'log' => [
            'driver' => 'log',
        ],
    ],

I also tried:

broadcast.php

    'connections' => [
        'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_KEY'),
            'secret' => env('PUSHER_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
        ],
        'redis' => [
            'driver' => 'redis',
            'connection' => 'pubsub',
        ],
        'log' => [
            'driver' => 'log',
        ],
    ],

.env

CACHE_DRIVER=memcached
SESSION_DRIVER=memcached
QUEUE_DRIVER=beanstalkd

BROADCAST_DRIVER=redis

And tried CACHE/SESSION drivers as Redis with pubsub set as redis connection in broadcast.php

Sorry to be a pain!

I can't find anything in Laravels docs regarding this...

Just look in the database config file. It's designed to be self explanatory, but can't be configued by default with the env file.

I've looked in the config file, it might have been designed to be self-explanatory but I still couldn't get it working.

This was my guess:

'redis' => [

        'cluster' => false,

        'default' => [
            'host'     => '127.0.0.1',
            'port'     => 6379,
            'database' => 0,
        ],
        'pubsub' => [
            'host'     => '127.0.0.1',
            'port'     => 6379,
            'database' => 1,
        ],
    ],

Which didn't work.

Any update regard this issue?
I'm experiencing the same issue while using event broadcasting driver & queuing driver as Redis.

@EwanValentine have you already fixed this issue?
If yes then let me know the process.

Not really unfortunately. I couldn't get the separate Redis db working. So
I ended up just using Pusher instead... Not ideal. There doesn't seem to be
much documentation around that. However, I'm aware it's probably my own
lack of understand around setting up Redis

On Mon, 14 Dec 2015 at 12:04 Sohel Amin [email protected] wrote:

@EwanValentine https://github.com/EwanValentine have you already fixed
this issue?
If yes then let me know the process.

—
Reply to this email directly or view it on GitHub
https://github.com/laravel/framework/issues/9398#issuecomment-164420709.

Thanks for the reply.

I've checked but laravel is not working with multiple connections.
May be we can't use redis for multiple operation at same time.

Fixed this issue by creating new redis instance with different connection.

$redis = Redis::connection('pubsub');

Oh you clever chap! That's just what I needed, thanks!
On Tue, 15 Dec 2015 at 14:49 Sohel Amin [email protected] wrote:

Fixed this issue by creating new redis instance with different connection.

$redis = Redis::connection('pubsub');

—
Reply to this email directly or view it on GitHub
https://github.com/laravel/framework/issues/9398#issuecomment-164785550.

Or, just tell broadcasting to use that connection... same thing.

event / pubsub specific connection.

app/config/broadcasting.php

<?php

return [
    'default' => env('BROADCAST_DRIVER', 'redis'),
    'connections' => [
        'redis' => [
            'driver' => 'redis',
            'connection' => 'pubsub',
        ]
    ],
];

multiple connections defined in database.php

app/config/database.php

    'redis' => [

        'cluster' => false,

        'pubsub' => [
            'host' => env('REDIS_HOST', 'localhost'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],

        'default' => [
            'host' => env('REDIS_HOST', 'localhost'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 1,
        ],

        'horizon' => [
            'host' => env('REDIS_HORIZON_HOST', env('REDIS_HOST', 'localhost')),
            'password' => env('REDIS_HORIZON_PASS', null),
            'port' => env('REDIS_HORIZON_PORT', 6379),
            'database' => 2,
        ],
    ],

where does this go? @sohelamin

$redis = Redis::connection('pubsub');

@jjhesk -
set it in the broadcasting connections.

<?php

return [
    'default' => env('BROADCAST_DRIVER', 'redis'),
    'connections' => [
        'redis' => [
            'driver' => 'redis',
            'connection' => 'pubsub',
        ]
    ],
];

Couldn't get this to work either... in my case. I'm using redis for broadcasting. Attempted to use redis for queue driver as well.

I tried defining multiple connections as @Artistan suggested. No Luck. I even tried spinning up a separate redis server on aws to point the second connection at a different IP Address (server). No Go.

For clarity:
I'm currently using redis for caching and for broadcast on Laravel 5.6 (works great). I'm trying to add Redis for my queue driver. As soon as I set the queue to use redis. It kills my broadcasting.

**Update: I think this is interesting if I change the queue driver to database. It kills my broadcasting as well. Which leads me to believe queue has to be sync in order for broadcast to be redis.

I want to give an update in case this helps someone else... In my case it was user error (Me). When you're broadcasting if your Event "implements ShouldBroadcast" by default that is using the queue driver. So if your queue driver is sync.. then of course executes immediately. If you change it to "redis" well then you better have a listener processing those jobs.. (LOL). In my case I was broadcasting and had a socket.io listener in JS on client side.. (Hopefully that makes sense).

You can in fact use redis for queue and broadcast just fine.. just take note of that ShouldBroadcast vs ShouldBroadcastNow.

like bodybag wrote, i searched for hours but the solved the problem by setting "QUEUE_DRIVER=sync" in my .env
just for clarification

Was this page helpful?
0 / 5 - 0 ratings