Saturday, February 24, 2018

Event not sending in real time laravel - development

Event not sending in real time laravel

I have situation.
I have event:

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

    public $conversation;
    public $user;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Conversation $conversation, User $user)
    {
        $this->conversation = $conversation;
        $this->user = $user;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return IlluminateBroadcastingChannel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('conversation.'.$this->conversation->id.'.'.$user->id);
    }}

In controller:

public function setMessagesSeen(Conversation $conversation) {

    $user = User::find(Auth::id());

    //$readed = Chat::conversations($conversation)->for($user)->readAll();

    //if($readed)
    broadcast(new MessageIsSeen($conversation, $user));

    //return json_encode(['lol']);
}

In app.js:

methods: {
    seenMessage() {
        //if(this.message.length != 0) {
            axios.post('/setMessagesSeen/' + this.convId)
            .then( response => { $('li').removeClass('unread'); }) 
            .catch( response => { console.log(response) } )
        //}
    }, //working. Send request to controller setMessageIsSeen
    mounted() {
        Echo.private('conversation.'+this.convId+'.'+ this.user.userId)
            .listen('MessageIsSeen', (e) => {
                console.log(e);
            });
    }

channels:

Broadcast::channel('conversation.{conversation_id}.{user_id}', function($user, $conversation_id, $user_id) {
    return $user;
});

In mounted event not listen. In console I didn’t have messages about event. In pusher I see messages only that channel: conversation.1.1 subscribed, occupied e.t.c. But messages about event MessageIsSeen – not. Please help fix it. In channels.php I have event.



from Laravel Questions and Answers https://laravelquestions.com/php/event-not-sending-in-real-time-laravel/
via Lzo Media

No comments:

Post a Comment