Laravel广播不会在前端提醒消息

时间:2019-11-22 05:31:55

标签: laravel pusher

我对这个laravel事件还很陌生,所以我无法正确完成它。

我正在尝试触发一个事件,以在作业完成时通知用户。所以,我在工作结束时触发了一个事件,

event(new AmenityUploaded($this->data['property_id']));

我的活动如下:

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class AmenityUploaded implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $property_id;

    public function __construct($property_id)
    {
        $this->property_id = $property_id;
    }

    public function broadcastOn()
    {
        return new PrivateChannel('channel.amenity.'.$this->property_id);
    }


    public function broadcastAs()
    {
        return 'amenity-uploaded';
    }
}

routes/channel.php

Broadcast::channel('channel.amenity.{propertyId}', function ($user, $propertyId) {
//    return $user->id === \App\Models\Property::find($propertyId)->updated_by;
    return true; 
}

而且,我认为javascript是:

视图

<script>

    Pusher.logToConsole = true;

    var pusher = new Pusher('xxxxxxxxxxxxxxxxxxxxx', {
        cluster: 'us3',
        forceTLS: true
    });
    $id = '<?php echo $r->id; ?>';
    var channel = pusher.subscribe(`channel.amenity.${$id}`);
    channel.bind('amenity-uploaded', function(data) {
        alert(JSON.stringify(data));
    });


</script>

我的.env如下:

BROADCAST_DRIVER=pusher

PUSHER_APP_ID=xxxxx
PUSHER_APP_KEY=xxxxxxxxxxxxxxxxxxxx
PUSHER_APP_SECRET=xxxxxxxxxxxxxxxxxxxx
PUSHER_APP_CLUSTER=us3

因此,每当我使用php artisan queue:work --tries=3运行队列时。最后,我可以看到App\Events\AmenityUploaded被解雇了。但是,我在前端看不到任何警报。

但是,如果我尝试从推送器仪表板推送通知,它将成功在应用程序上成功发出警报。从Pusher调试控制台推送时,我将频道用作channel.amenity.1,将事件用作amenity-uploaded

## Update

我在事件中添加了一个broadcastWith块:

    public function broadcastWith()
    {
        // This must always be an array. Since it will be parsed with json_encode()
        return [
            'name' => 'Saroj',
            'message' => 'Message',
        ];
    }

而且,这已成功显示在推送器仪表板中。但是,尚未在前端发出警报。

screenshot

更新2

尝试回显:在bootstrap.js

中添加了以下代码
import Echo from 'laravel-echo'
import Pusher from "pusher-js"
window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'xxxxxxxxxxxxxxxxxxxx',
    cluster: 'us3',
    encrypted: true
});

在视野中:

<script>
    $id = '<?php echo $r->id; ?>';


    Echo.channel(`channel.amenity.${$id}`)
        .listen('.amenity-uploaded', (data) => {
            alert(JSON.stringify(data));
        });


</script>

还是一样,如果从推送器仪表板触发,则警报将显示在前端。完成工作后,可以在推杆仪表板上看到该事件,但是在前端却看不到。

1 个答案:

答案 0 :(得分:0)

我认为问题在于,您仅在前端使用Pusher,而没有laravel-echo才能使用私有渠道。 Pusher本身并不知道它必须授权,laravel-echo可以直接使用。

这就是我初始化PusherEcho的方式:

window.Pusher = require('pusher-js');
window.Echo = new Echo({
    broadcaster: 'pusher',
    key: fromPHP.PUSHER_APP_KEY,
    cluster: fromPHP.PUSHER_CLUSTER,
    encrypted: true
});

这是我监听私人活动的方式:

Echo.private(channel).listen('.amenity-uploaded', data => alert(JSON.stringify(data)));

请注意前.前的amenity-uploaded。自定义broadcastAshttps://laravel.com/docs/5.8/broadcasting#broadcast-name

时,这是必需的