laravel-websockets icon indicating copy to clipboard operation
laravel-websockets copied to clipboard

Get user data in websocket handlers

Open S1ipKn0T opened this issue 2 years ago • 0 comments

This is my onClose function code in WebSocketHandler.php file:

 public function onClose(ConnectionInterface $connection)
    {

        $allChannels = $this->channelManager->getChannels($connection->app->id);

        foreach ($allChannels as $channelData) {
            $data = $channelData;
        }
        dd($data);

        $this->channelManager->removeFromAllChannels($connection);

        DashboardLogger::disconnection($connection);

        StatisticsLogger::disconnection($connection);
    }

and when the user closes the tab or browser my onClose function gives me $data in console logging which is this response:

BeyondCode\LaravelWebSockets\WebSockets\Channels\PresenceChannel {#719
  #users: array:1 [
    1 => array:9 [
      "id" => 1
      "email" => "[email protected]"
      "username" => "BlackHill"
      "type" => 0
      "steam_id" => "76561198107211703"
      "stay_login" => 0
      "email_verified_at" => null
      "created_at" => "2022-03-21T22:54:53.000000Z"
      "updated_at" => "2022-04-23T20:13:19.000000Z"
    ]
  ]
  #sockets: array:1 [
    "161595151.817952840" => "1"
  ]
  #channelName: "presence-users"
}

I need to access #users array to apply changes to any user who leaves. In this issue they managed to get channelName but I did not get #users using their solution!

S1ipKn0T avatar May 11 '22 11:05 S1ipKn0T