beyondcode / laravel-websockets

Websockets for Laravel. Done right.
https://beyondco.de/docs/laravel-websockets
MIT License
5.08k stars 628 forks source link

How can I catch messages from websockets without custom handler? #664

Closed Alexxosipov closed 3 years ago

Alexxosipov commented 3 years ago

I read all the documentation that I found on https://beyondco.de/docs/laravel-websockets/ and I didn't find any information about handling incoming messages from websocket on backend. Is there a way to handle it without custom handler? Or the only way is to create my own handler? If yes, is it possible to create 2 different websockets: one for default Laravel notifications that sending via private channels and another for receiving messages?

simonbuehler commented 3 years ago

in 2.x you can use the BeyondCode\LaravelWebSockets\Events\WebSocketMessageReceive Event, see https://github.com/beyondcode/laravel-websockets/blob/2.x/docs/advanced-usage/dispatched-events.md

yaofong commented 3 years ago

Hi, I could trigger the WebSocketMessageReceive event, however I could not get the payload data. Anyone tried this before?

simonbuehler commented 3 years ago

pls show some code and a dump of the $eventin the handle method

yaofong commented 3 years ago

pls show some code and a dump of the $eventin the handle method

added to EventServiceProvider.php

WebSocketMessageReceived::class => [ HandleWebSocketMessage::class, ]

in HandleWebSocketMessage.php

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Log;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use BeyondCode\LaravelWebSockets\Events\WebSocketMessageReceived;

class HandleWebSocketMessage
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  WebSocketMessageReceived  $event
     * @return void
     */
    public function handle(WebSocketMessageReceived $event)
    {
        Log::info(json_encode($event, true));
    }
}

Log:

local.INFO: {"appId":"CROWDCONNECT","socketId":"915320443.338638457","message":{},"decodedMessage":{"event":"pusher:subscribe","data":{"auth":"","channel":"kitchen-channel"}}}

From websocket console: Screen Shot 2021-02-02 at 4 53 44 PM

simonbuehler commented 3 years ago

ok the key point is the difference between received and sent messages.

i guess you want to monitor the Messages from the server to the Client?

yaofong commented 3 years ago

ok the key point is the difference between received and sent messages.

i guess you want to monitor the Messages from the server to the Client?

Yup, I want to get the message sent, and save it to db as a log.

zzmasoud commented 3 years ago

Hi everyone, I did use this listener to update user offline/online status:

public function handle(WebSocketMessageReceived $event)
    {
        $object = $event->decodedMessage;
        $eventName = $object['event'];

        if ($eventName == "pusher:subscribe" || $eventName == "pusher:unsubscribe") {
            $channel = $object['data']['channel'];
            $userId = $this->getUserIdFromChannelName($channel);
            if (isset($userId)) { 
                $isOnline = $eventName == "pusher:subscribe";
                $this->processUserStats($userId, $isOnline);                
            }
        }
    }

everything working fine, like updating model in DB, at the end of handle event I want to broadcast user updated status to peers, I have error on broadcasting a message in here, anywhere else it's working fine. any suggestion?

broadcast(new ProfileUpdate($userIds, new UserResource($user)));

that's the error:

Exception `Illuminate\Broadcasting\BroadcastException` thrown: `Pusher error: .`
1215394: exception `Illuminate\Broadcasting\BroadcastException` thrown: `Pusher error: .`.

 [2021-10-24 18:49:43] local.ERROR: exec_curl error: {error} {"error":"Operation timed out after 30000 milliseconds with 0 bytes received"} 
[2021-10-24 18:49:43] local.INFO: exec_curl response: {response} {"response":"Array
(
    [body] => 
    [status] => 0
)

"}

mounir-1999 commented 1 year ago

@yaofong, Did you find a solution for your case, I am facing same issue