简体   繁体   中英

Vue/Laravel Websockets Leave

I'm trying to detect when client is disconnected from websocket and trigger some function with it, but so far I had no success. I'm using BeyondCode websockets and the way I'm calling class is like:

Broadcast::channel('order.{id}', OrderChannel::class);

So this OrderChannel class has function

public function join(User $user, Orders $id)
    {
        return ['id' => $user->id, 'name' => $user->name, 'image' => $user->profile_photo];
    }

and it's working as expected, but I would like to update something in database when user disconnect from that socket, and I tried something like:

public function leave(User $user, Orders $id)
    {
        $order = Orders::where('id', $id)->withTrashed()->update(['user_id' => null]);

        return $order;
    }

But it's not working, any suggestions how to trigger this?

I think that Presence channels should solve your problem. You can find documentation about this on: https://laravel.com/docs/8.x/broadcasting#presence-channels .

You can then subscribe to the leaving event, and trigger your back-end by sending an HTTP request for example, and executing the code currently in your leave function.

It is difficult to say, without seeing a more elaborate example on how you're subscribing to the event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM