diff --git a/CHANGELOG.md b/CHANGELOG.md index a122cf78..26907098 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ - Change the behavior of the default generated collections snapshot migration to act as "extend" instead of "replace" to prevent accidental data deletion. _I think this would be rare but if you want the old behaviour you can edit the generated snapshot file and replace the second argument (`deleteMissing`) of `App.ImportCollection/App.ImportCollectionsByMarshaledJSON` from `false` to `true`._ +- Added `app.SubscriptionsBroker().TotalClients()` helper method to return the total registered realtime clients ([#5793](https://github.com/pocketbase/pocketbase/issues/5793)). + ## v0.23.0-rc9 diff --git a/tools/subscriptions/broker.go b/tools/subscriptions/broker.go index 3b080a3c..21a4eb85 100644 --- a/tools/subscriptions/broker.go +++ b/tools/subscriptions/broker.go @@ -30,6 +30,11 @@ func (b *Broker) ChunkedClients(chunkSize int) [][]Client { return list.ToChunks(b.store.Values(), chunkSize) } +// TotalClients returns the total number of registered clients. +func (b *Broker) TotalClients() int { + return b.store.Length() +} + // ClientById finds a registered client by its id. // // Returns non-nil error when client with clientId is not registered. diff --git a/tools/subscriptions/broker_test.go b/tools/subscriptions/broker_test.go index 9c5d1ed8..74955d60 100644 --- a/tools/subscriptions/broker_test.go +++ b/tools/subscriptions/broker_test.go @@ -62,6 +62,21 @@ func TestChunkedClients(t *testing.T) { } } +func TestTotalClients(t *testing.T) { + b := subscriptions.NewBroker() + + if total := b.TotalClients(); total != 0 { + t.Fatalf("Expected no clients, got %d", total) + } + + b.Register(subscriptions.NewDefaultClient()) + b.Register(subscriptions.NewDefaultClient()) + + if total := b.TotalClients(); total != 2 { + t.Fatalf("Expected %d clients, got %d", 2, total) + } +} + func TestClientById(t *testing.T) { b := subscriptions.NewBroker()