channelManager = $channelManager; } function onOpen(ConnectionInterface $connection) { $this->generateSocketId($connection); $this->verifyConnection($connection); $this->establishConnection($connection); } public function onMessage(ConnectionInterface $connection, MessageInterface $message) { $message = RespondableMessageFactory::createForMessage($message, $connection, $this->channelManager); $message->respond(); } public function onClose(ConnectionInterface $connection) { $this->channelManager->removeFromAllChannels($connection); } function onError(ConnectionInterface $connection, Exception $exception) { if ($exception instanceof PusherException) { $connection->send(json_encode( $exception->getPayload() )); } } protected function verifyConnection(ConnectionInterface $connection) { /** @var \GuzzleHttp\Psr7\Request $request */ $request = $connection->httpRequest; $queryParameters = []; parse_str($request->getUri()->getQuery(), $queryParameters); if (! $client = Client::findByAppKey($queryParameters['appKey'])) { throw new UnknownAppKeyException($queryParameters['appKey']); } $connection->client = $client; } protected function establishConnection(ConnectionInterface $connection) { $connection->send(json_encode([ 'event' => 'pusher:connection_established', 'data' => json_encode([ 'socket_id' => $connection->socketId, 'activity_timeout' => 60, ]) ])); } protected function generateSocketId(ConnectionInterface $connection) { $socketId = sprintf("%d.%d", getmypid(), random_int(1, 100000000)); $connection->socketId = $socketId; } public function log(string $appId, string $type, string $details) { $channelId = "private-logger-{$type}"; $channel = $this->channelManager->find($appId, $channelId); optional($channel)->broadcast([ 'event' => $type, 'channel' => $channelId, 'data' => [ 'type' => $type, 'details' => $details ] ]); } }