My broadcasts worked fine yesterday. I don't think anything changed, but now they don't work.
I have installed reverb, and with the server running in debug mode, I can see that events are fired to both my channels (private-users.1 and public-notifications).
I have the broadcasts running via a notification. The notification gets logged to the database. When implementing logging, I can also see that all the broadcast functions are being run.
I have my queues running, and I can see that the notification runs.
The only difference I see today is that "Illuminate\Notifications\Events\BroadcastNotificationCreated" shows up in my queue as well. I don't know why that is now showing up, but it seems like that is causing the issue.
I've also changed my broadcastdriver to "log" and can see the notification in my logs at that time.
Any thoughts?
Here is my set-up:
Echo.js
Import Echo from 'laravel-echo';
import Pusher from 'pusher-js';
window.Pusher = Pusher;
window.Echo = new Echo({
broadcaster: 'reverb',
key: import.meta.env.VITE_REVERB_APP_KEY,
wsHost: import.meta.env.VITE_REVERB_HOST,
wsPort: import.meta.env.VITE_REVERB_PORT ?? 80,
wssPort: import.meta.env.VITE_REVERB_PORT ?? 443,
forceTLS: (import.meta.env.VITE_REVERB_SCHEME ?? 'https') === 'https',
enabledTransports: ['ws', 'wss'],
});
document.addEventListener('DOMContentLoaded', function () {
const userID = window.userID;
window.Echo.private(`users.`+userID)
.listen('.user.notification', (e) => {
console.log('user');
buildMessage(e);
if(e.database !== 'undefined' && e.database) {
addNotification(e);
}
});
window.Echo.channel(`public-updates`)
.listen('.user.notification', (e) => {
alert('hree');
console.log('public update');
});
});
Channels route file
use Illuminate\Support\Facades\Broadcast;
Broadcast::channel('users.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
Broadcast::channel('public-updates', function () {
return true;
});
.env
BROADCAST_DRIVER=reverb
REVERB_APP_ID=idhere
REVERB_APP_KEY=keyhere
REVERB_APP_SECRET=secrethere
REVERB_HOST="localhost"
REVERB_PORT=8080
REVERB_SCHEME=http
VITE_REVERB_APP_KEY="${REVERB_APP_KEY}"
VITE_REVERB_HOST="${REVERB_HOST}"
VITE_REVERB_PORT="${REVERB_PORT}"
VITE_REVERB_SCHEME="${REVERB_SCHEME}"
Notification
class SendSiteNotification extends Notification implements ShouldQueue, ShouldBroadcastNow
{
use Queueable;
private string $user_id;
/**
* @param string $message
* @param array|null $variables
* @param string|null $type
*/
public function __construct(
private string $message,
private ?array $variables= [],
private ?string $type = 'success'
)
{
// nothing
}
/**
* @param object $notifiable
* @return string[]
*/
public function via(object $notifiable): array
{
$this->user_id = $notifiable->id;
return ['broadcast'];
}
/**
* @param object $notifiable
* @return array[]
*/
public function toArray(object $notifiable): array
{
return array_merge(
[
'message' => [
'text' => $this->message,
'type' => $this->type,
]
],
$this->variables
);
}
public function toBroadcast($notifiable): BroadcastMessage {
return new BroadcastMessage($this->toArray($notifiable));
}
public function broadcastType(): string {
return 'user.notification';
}
public function broadcastOn(): array
{
return [
new Channel('users.'.$this->user_id)
];
}
}