diff --git a/src/Servers/ApiGateway/Connection.php b/src/Servers/ApiGateway/Connection.php index 3ea91b7d..928c4eaa 100644 --- a/src/Servers/ApiGateway/Connection.php +++ b/src/Servers/ApiGateway/Connection.php @@ -8,6 +8,8 @@ class Connection implements WebSocketConnection { + protected bool $isFresh = true; + /** * Create a new connection instance. */ @@ -29,15 +31,15 @@ public function id(): int|string */ public function send(mixed $message): void { - info('Sending message to connection', [ - 'connection' => $this->identifier, - 'message' => $message, - ]); - - app()->terminating(fn () => SendToConnection::dispatch( - $this->identifier, - $message - )); + if ($this->isFresh()) { + SendToConnection::dispatch($this->identifier, $message); + + $this->isFresh = false; + + return; + } + + SendToConnection::dispatchSync($this->identifier, $message); } /** @@ -47,4 +49,12 @@ public function close(mixed $message = null): void { app(ConnectionManager::class)->forgetById($this->identifier); } + + /** + * Determine if the connection was instantiated during the current request. + */ + protected function isFresh(): bool + { + return $this->isFresh; + } } diff --git a/src/Servers/ApiGateway/Jobs/SendToConnection.php b/src/Servers/ApiGateway/Jobs/SendToConnection.php index 4555e9b2..33343ff0 100644 --- a/src/Servers/ApiGateway/Jobs/SendToConnection.php +++ b/src/Servers/ApiGateway/Jobs/SendToConnection.php @@ -3,12 +3,13 @@ namespace Laravel\Reverb\Servers\ApiGateway\Jobs; use Aws\ApiGatewayManagementApi\ApiGatewayManagementApiClient; +use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Support\Facades\Config; use Laravel\Reverb\Loggers\Log; use Throwable; -class SendToConnection +class SendToConnection implements ShouldQueue { use Dispatchable; @@ -25,11 +26,6 @@ public function __construct(public string $connectionId, public string $message) */ public function handle(): void { - info('Sending message to connection.', [ - 'connectionId' => $this->connectionId, - 'message' => $this->message, - ]); - try { $client = new ApiGatewayManagementApiClient([ 'region' => Config::get('reverb.servers.api_gateway.region'), diff --git a/src/Servers/ApiGateway/Server.php b/src/Servers/ApiGateway/Server.php index c91b9ef7..1d9bcc35 100644 --- a/src/Servers/ApiGateway/Server.php +++ b/src/Servers/ApiGateway/Server.php @@ -36,15 +36,12 @@ public function handle(Request $request): void $this->connect($request), ), 'MESSAGE' => $this->server->message( - $this->connect($request), - $connection = $request->message() + $connection = $this->connect($request), + $request->message() ), }; } catch (InvalidApplication $e) { - app()->terminating(fn () => SendToConnection::dispatch( - $request->connectionId(), - $e->getMessage() - )); + SendToConnection::dispatch($request->connectionId(), $e->getMessage()); } catch (\Exception $e) { $this->server->error( $this->connect($request), @@ -53,7 +50,7 @@ public function handle(Request $request): void } if (isset($connection)) { - app()->terminating(fn () => app(ConnectionManager::class)->update($connection)); + app(ConnectionManager::class)->update($connection); } }