lundi 5 juin 2017

Laravel Echo isn't listening to the Broadcast event

I am trying to broadcast the event when a user creates a post to all his friends. Laravel Echo isn't listening to the events. I can see the logs on the Pusher site. So the event is being sent to the pusher.

Here's my timeline code:

<template>
<div>
    <div class="row">
        <status-box :authUser="authUser"></status-box>
    </div>

    <div class="row">
        <div class="col-lg-8">
            <div v-if="statuses.length">
                <status v-for="status in statuses" :status="status"></status>
            </div>
            <div v-else>
                <p>No posts to show yet.</p>
            </div>
        </div>
    </div>
</div>

<script>
import EventHub from './../eventhub.js';

export default {

    data() {
        return {
            statuses: [],
            authUser: null,
        };
    },

    methods: {
        pushNewStatus(status) {
            status.parent_id = null;
            this.statuses.unshift(status);

            // Broadcasting the event to so that user's friends can see the post in realtime
            Echo.private('status.' + status.id)
                .listen('StatusWasPosted', (e) => {
                    this.pushNewStatus(e.status);
                });
        }
    },

    mounted() {
        EventHub.$on('status-posted', this.pushNewStatus)

        axios.get('/chatty/getStatuses/')
            .then(({data}) => {
                this.statuses = data.statuses;
                this.authUser = data.user_name;
            })
            .catch(error => console.log(error));
    }

}

Here's the channel authorization logic:

Broadcast::channel('status.{statusId}', function ($user, $statusId) {
$status = Status::find($statusId);
$status_owner = User::find($status->user_id);
return $user->isFriendsWith($status_owner);

});

Here's the channel name in the StatusWasPosted event class

public function broadcastOn() {
    return new PrivateChannel('status.' . $this->status->id);

}

I am emitting status-posted in the status compoenet when a new status is created.

Where am I going wrong??



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire