diff --git a/config/websockets.php b/config/websockets.php index 693f861..caa8507 100644 --- a/config/websockets.php +++ b/config/websockets.php @@ -42,10 +42,19 @@ return [ */ 'max_request_size_in_kb' => 250, - /* - * This model will be used to store the statistics of the WebSocketsServer - */ - 'statistics_model' => \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry::class, + 'statistics' => [ + /* + * This model will be used to store the statistics of the WebSocketsServer. + * The only requirement is that the model should be or extend + * `WebSocketsStatisticsEntry` provided by this package. + */ + 'model' => \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry::class, + + /* + * Here you can specify the interval in seconds at which statistics should be logged. + */ + 'interval_in_seconds' => 60, + ], /* * Define the optional SSL context for your WebSocket connections. diff --git a/src/Console/StartWebSocketServer.php b/src/Console/StartWebSocketServer.php index 2d6363a..2778c5c 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -57,7 +57,7 @@ class StartWebSocketServer extends Command return new HttpStatisticsLogger(app(ChannelManager::class), $browser); }); - $this->loop->addPeriodicTimer(5, function() { + $this->loop->addPeriodicTimer(config('websockets.statistics.interval_in_seconds'), function() { StatisticsLogger::save(); }); diff --git a/src/Statistics/Events/StatisticsUpdated.php b/src/Statistics/Events/StatisticsUpdated.php index 128a25b..d6fa70b 100644 --- a/src/Statistics/Events/StatisticsUpdated.php +++ b/src/Statistics/Events/StatisticsUpdated.php @@ -3,6 +3,7 @@ namespace BeyondCode\LaravelWebsockets\Statistics\Events; use BeyondCode\LaravelWebSockets\Dashboard\DashboardLogger; +use BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry; use Illuminate\Broadcasting\PrivateChannel; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; use Illuminate\Queue\SerializesModels; @@ -11,27 +12,30 @@ class StatisticsUpdated implements ShouldBroadcast { use SerializesModels; - protected $statisticModel; + /** @var \BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry */ + protected $webSocketsStatisticsEntry; - public function __construct($statisticModel) + public function __construct(WebSocketsStatisticsEntry $webSocketsStatisticsEntry) { - $this->statisticModel = $statisticModel; + $this->webSocketsStatisticsEntry = $webSocketsStatisticsEntry; } public function broadcastWith() { return [ - 'time' => $this->statisticModel->created_at->timestamp, - 'app_id' => $this->statisticModel->appId, - 'peak_connection_count' => $this->statisticModel->peakConnectionCount, - 'websocket_message_count' => $this->statisticModel->webSocketMessageCount, - 'api_message_count' => $this->statisticModel->apiMessageCount, + 'time' => $this->webSocketsStatisticsEntry->created_at->timestamp, + 'app_id' => $this->webSocketsStatisticsEntry->appId, + 'peak_connection_count' => $this->webSocketsStatisticsEntry->peakConnectionCount, + 'websocket_message_count' => $this->webSocketsStatisticsEntry->webSocketMessageCount, + 'api_message_count' => $this->webSocketsStatisticsEntry->apiMessageCount, ]; } public function broadcastOn() { - return new PrivateChannel(str_after(DashboardLogger::LOG_CHANNEL_PREFIX . 'statistics', 'private-')); + $channelName = str_after(DashboardLogger::LOG_CHANNEL_PREFIX . 'statistics', 'private-'); + + return new PrivateChannel($channelName); } public function broadcastAs() diff --git a/src/Statistics/Http/Controllers/WebSocketStatisticsEntriesController.php b/src/Statistics/Http/Controllers/WebSocketStatisticsEntriesController.php index 2279a0e..f898b2e 100644 --- a/src/Statistics/Http/Controllers/WebSocketStatisticsEntriesController.php +++ b/src/Statistics/Http/Controllers/WebSocketStatisticsEntriesController.php @@ -17,7 +17,7 @@ class WebSocketStatisticsEntriesController 'api_message_count' => 'required|integer', ]); - $webSocketsStatisticsEntryModelClass = config('websockets.statistics_model'); + $webSocketsStatisticsEntryModelClass = config('websockets.statistics.model'); $statisticModel = $webSocketsStatisticsEntryModelClass::create($validatedAttributes);