framework: Redis not picking up Broadcast events
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 <ewan@theladbible.com>
* @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!
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 21 (4 by maintainers)
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
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.
Fixed this issue by creating new redis instance with different connection.