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⠀⠀⠀⠀⠀⠀ | authed | purpose |
---|---|---|
user:public | maybe | All 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> | yes | Used to broadcast changes that affect a particular user (ex: logout) |
user:role | yes | Used to broadcast when a role has its permissions changed |
client-handled-messages-messages-broadcast-from-server
Client Handled Messages (messages broadcast from server)
broadcast message | channel⠀⠀⠀⠀⠀⠀ | payload⠀⠀⠀⠀⠀⠀ | client action |
---|---|---|---|
announcement | user:public | %{} | fetches MOTD announcement |
reauthenticate | user:<user_id> | %{} | reauthenticates, fetches user changes |
logout | user:<user_id> | %{:token} | logout all user sessions with token |
newMessage | user:<user_id> | %{} | fetches new messages/counts |
refreshMentions | user:<user_id> | %{} | fetches new mentions/counts |
permissionsChanged | user:role | %{} | reauthenticates, fetches new permissions |
server-handled-messages-messages-broadcast-from-client
Server Handled Messages (messages broadcast from client)
broadcast message | channel | payload⠀⠀⠀⠀⠀⠀ | server action |
---|---|---|---|
is_online | user: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
child_spec(init_arg)
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.
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.
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.
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
.
start_link(triplet)
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.