GameServerWeb.ConnectionTracker (game_server_web v1.0.940)

Copy Markdown

Tracks active WebSocket channel and WebRTC peer connections using a Registry with :duplicate keys.

Each channel process registers itself on join via register/2. Because Registry links to the registering process, entries are automatically removed when the process terminates — no manual cleanup needed.

Connection types

  • :ws_socket — raw WebSocket connections (1 per client)
  • :user_channel — authenticated user WebSocket channels
  • :lobby_channel — per-lobby channels (members + spectators)
  • :lobbies_channel — global lobbies listing channel
  • :group_channel — per-group channels
  • :groups_channel — global groups listing channel
  • :party_channel — per-party channels
  • :live_view — active LiveView connections (browser tabs)
  • :webrtc_peer — active WebRTC DataChannel peers

Summary

Functions

Returns a map with counts for every tracked connection type plus totals. In a clustered environment, this only counts connections on the local node. Use cluster_counts/0 for aggregated counts across all nodes.

Returns all connection types and their registered processes.

Returns the child spec to start the underlying Registry. Add this to your supervision tree before the Endpoint.

Returns aggregated connection counts across all connected nodes in the cluster. Falls back to local-only counts if the cluster has a single node.

Returns the count of processes registered under type. Returns 0 if the registry hasn't been started yet.

Formats uptime seconds into a human-readable string like "2d 3h 15m".

Returns a list of {pid, metadata} tuples for all processes registered under the given type.

Registers the calling process under the given connection type. Optional metadata map is stored alongside the registration.

Returns BEAM system statistics useful for the admin dashboard.

Functions

all_counts()

Returns a map with counts for every tracked connection type plus totals. In a clustered environment, this only counts connections on the local node. Use cluster_counts/0 for aggregated counts across all nodes.

all_registered()

Returns all connection types and their registered processes.

child_spec(opts)

Returns the child spec to start the underlying Registry. Add this to your supervision tree before the Endpoint.

cluster_counts()

Returns aggregated connection counts across all connected nodes in the cluster. Falls back to local-only counts if the cluster has a single node.

count(type)

Returns the count of processes registered under type. Returns 0 if the registry hasn't been started yet.

format_uptime(seconds)

Formats uptime seconds into a human-readable string like "2d 3h 15m".

list_registered(type)

Returns a list of {pid, metadata} tuples for all processes registered under the given type.

register(type, metadata \\ %{})

Registers the calling process under the given connection type. Optional metadata map is stored alongside the registration.

Returns {:ok, pid} or {:error, term}. No-op if the registry hasn't been started yet.

system_stats()

Returns BEAM system statistics useful for the admin dashboard.