GamendWeb.UserChannel (gamend_web v1.0.1215)

Copy Markdown View Source

Channel for sending per-user realtime updates (e.g. metadata changes).

Topic: "user:<user_id>" Clients must authenticate the socket connection (JWT) and may only join topics belonging to their own user id.

Online presence

When a user joins the channel their is_online flag is set to true in the database and a "friend_updated" event is pushed to every accepted friend's channel. When the last channel process for a user terminates the flag is reset and pushed through the same "friend_updated" event.

Notifications

On join the channel pushes all undeleted notifications for the user in chronological order (oldest-first) as individual "notification_created" events. New notifications arriving while connected are also pushed as "notification_created".

Hook RPC

Clients can call plugin hooks via "call_hook" push with reply:

push("call_hook", %{plugin: "my_plugin", fn: "my_func", args: [1, 2]})
 reply {:ok, %{data: result}} | {:error, %{error: reason}}

This avoids the HTTP round-trip of POST /api/v1/hooks/call while the socket is connected. The caller context (user) is injected automatically.

KV RPC

Clients can subscribe to a key/scope and receive "kv_updated" / "kv_deleted" pushes when that KV entry changes:

push("kv:subscribe", %{key: "my_key", user_id: 123})
-> reply {:ok, %{subscribed: true, ...}}

Summary

Functions

child_spec(init_arg)

start_link(triplet)