0
votes

I'm trying to build a chat application and for that, I'm using Laravel's Echo and Laravel echo server. Idea is to listen for a message notification.

I have done everything according to the:

https://laravel.com/docs/master/notifications

https://laravel.com/docs/master/broadcasting

resources/assets/js/bootstrap.js: (of course I have complied it down using NPM)

import Echo from 'laravel-echo'

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001'
});

laravel-echo-server.json:

{
    "authHost": "http://laravel-notifications.local",
    "authEndpoint": "/broadcasting/auth",
    "clients": [],
    "database": "redis",
    "databaseConfig": {
        "redis": {},
        "sqlite": {
            "databasePath": "/database/laravel-echo-server.sqlite"
        }
    },
    "devMode": true,
    "host": null,
    "port": "6001",
    "protocol": "http",
    "socketio": {},
    "sslCertPath": "",
    "sslKeyPath": ""
}

ChatController.php (To fire a notification)

$toUser = User::find($toUser)->notify(new MessageNotification($message));

MessageNotification.php

public function toBroadcast($notifiable)
{
      return new BroadcastMessage([
        'message' => $notifiable,
        ]);
}

Blade view ( Listening for notification):

window.Echo.private('App.User.' + window.Laravel.user.id)
    .notification((notification) => {
        console.log(notification); // Not getting executing 
});

When I visit the page, I see the following log from the Laravel Echo Server:

[9:34:41 AM] - XnmH1wzMJM-5VON-AAAA authenticated for: private-App.User.7

[9:34:41 AM] - XnmH1wzMJM-5VON-AAAA joined channel: private-App.User.7

But when I'm firing that notification, I don't see anything logged on the browser console.

Complete source code:

https://github.com/xparthxvorax/Laravel-Notifications

NB: I'm successfully able to listen to the event but not notifications.

1

1 Answers

0
votes

In the notification, in the broadcasting channel part, you need to specify the queue that will dispatch the broadcasting. For example:

public function toBroadcast($notifiable)
{
    return (new BroadcastMessage([
        'html' => $data
    ]))->onConnection('database')->onQueue('alerts');
}