EpochtalkServerWeb.UserChannel (epochtalk_server v0.1.0)

Handles User websocket channels. These channels are used to broadcast events to the client. With the current design, API route can broadcast messages when an action is performed, this notifies the client to perform an action, such as requesting data from the server.

TODO(akinsey): This is a direct port of the old epochtalk websocket architecture, there is room for improvement. For certain messages (ex: announcement) relevant data can be sent over the channel via the broadcast message payload. Currently, receiving a message triggers the client to manually make an api request for the updated data.

supported-user-channels

Supported User Channels

channel⠀⠀⠀⠀⠀⠀authedpurpose
user:publicmaybeAll logged in and anonymous users are connected to this channel. Used to track user's online status and to broadcast MOTD announcements.
user:<user_id>yesUsed to broadcast changes that affect a particular user (ex: logout)
user:roleyesUsed to broadcast when a role has its permissions changed

client-handled-messages-messages-broadcast-from-server

Client Handled Messages (messages broadcast from server)

broadcast messagechannel⠀⠀⠀⠀⠀⠀payload⠀⠀⠀⠀⠀⠀client action
announcementuser:public%{}fetches MOTD announcement
reauthenticateuser:<user_id>%{}reauthenticates, fetches user changes
logoutuser:<user_id>%{:token}logout all user sessions with token
newMessageuser:<user_id>%{}fetches new messages/counts
refreshMentionsuser:<user_id>%{}fetches new mentions/counts
permissionsChangeduser:role%{}reauthenticates, fetches new permissions

server-handled-messages-messages-broadcast-from-client

Server Handled Messages (messages broadcast from client)

broadcast messagechannelpayload⠀⠀⠀⠀⠀⠀server action
is_onlineuser:public%{:user_id}replies to client with the specified user's online status

Link to this section Summary

Functions

Handles message is_online, checks user:public channel to see if User with user_id is connected using Presence. Returns user_id and online, a boolean indicating if the User is connected.

Handles joining of user:public channel. Message is broadcast on this channel when a MOTD is updated, this tells the client to fetch the MOTD.

Handles joining of user:role channel. Messages are broadcast on this channel when a roles are updated, which tell the client to reauthenticate inorder to fetch new roles. Messages contain the lookup of the updated role, the client will check the user's roles for the role lookup and reauthenticate if necessary.

Handles joining of user:<user_id> channel, enforces that user is authenticated. This channel is used to broadcast events such as reauthenticate or logout to a specific User with matching user_id.

Tracks a User that joins a channel using Presence. Currently used to track a User that joins the user:public channel. Used by forum order to keep track of online users.

Link to this section Functions

Link to this function

child_spec(init_arg)

Link to this function

is_online(socket, user_id)

@spec is_online(socket :: Phoenix.Socket.t(), user_id :: non_neg_integer()) ::
  {:reply, {:ok, %{id: non_neg_integer(), online: boolean()}},
   socket :: Phoenix.Socket.t()}

Handles message is_online, checks user:public channel to see if User with user_id is connected using Presence. Returns user_id and online, a boolean indicating if the User is connected.

Link to this function

join_public_channel(socket)

@spec join_public_channel(socket :: Phoenix.Socket.t()) :: {:ok, Phoenix.Socket.t()}

Handles joining of user:public channel. Message is broadcast on this channel when a MOTD is updated, this tells the client to fetch the MOTD.

Link to this function

join_role_channel(socket)

@spec join_role_channel(socket :: Phoenix.Socket.t()) ::
  {:ok, Phoenix.Socket.t()} | {:error, data :: map()}

Handles joining of user:role channel. Messages are broadcast on this channel when a roles are updated, which tell the client to reauthenticate inorder to fetch new roles. Messages contain the lookup of the updated role, the client will check the user's roles for the role lookup and reauthenticate if necessary.

Link to this function

join_user_channel(socket, user_id)

@spec join_user_channel(socket :: Phoenix.Socket.t(), user_id :: String.t()) ::
  {:ok, Phoenix.Socket.t()} | {:error, data :: map()}

Handles joining of user:<user_id> channel, enforces that user is authenticated. This channel is used to broadcast events such as reauthenticate or logout to a specific User with matching user_id.

Link to this function

start_link(triplet)

Link to this function

track_user_online(socket)

@spec track_user_online(socket :: Phoenix.Socket.t()) ::
  {:noreply, Phoenix.Socket.t()}

Tracks a User that joins a channel using Presence. Currently used to track a User that joins the user:public channel. Used by forum order to keep track of online users.