Hi everybody
I have a general question about live view and sockets.
We have umbrella porject with several apps in it,: core/backoffice/client/superadmin etc
In the client’s app we have an active event page where users can communicate. It’s quite similar to chat or webinar. There is couple of live-components: user’s list, shared canvas, chat, shared documents etc.
On the backend there is a live console where admin can manage this process: he can see active users, attach documents and send some system notifications to the shared rooms.
Right now there is EventChannel to handle when user’s connect to room via sockets.
So, in app.js we have connection to LiveSocket to handle phoenix live view events and there is connection to custom event’s channel (via phx-hooks)
Live view and specific channel are 2 different processes and they can’t communicate with each other like live-components inside of one live view. For that purpose we need to use PubSub. right?
Live Console at backoffice subscribes to the event’s topic, let’s say: “event:#{id}”, on BackOffice.PubSub
Client’s Live View subscribes to the same topic as well, but on ClientApp.PubSub
Client’s channel uses ClientApp.PubSub as well, so it automatically subscribe to the same topic if it’s configured in user’s socket.
channel “event:*”, ClientApp.EventChannel
In channel implemented tracking of presence, sending events :user_joined/:user_left to both pub-subs, so backoffice and client’s live view can update who is online.
When I want to send an update from backoffice to client only to live view, I make a call to helper function
app_broadcast(socket, ClientApp.PubSub, source, event, payload)
def app_broadcast(socket, to, source, event, payload) do
if !is_nil(Process.whereis(to)) do
PubSub.broadcast to, topic(source), {event, payload}
end
socket
end
From channel, when I want to send event to both, I just make a call
socket
|> app_broadcast(ClientApp.PubSub, source, event, payload)
|> app_broadcast(BackOffice.PubSub, source, event, payload)
But channel receive a message as well and I don’t need to listen to all events in channel and live view at same time.
My question is more about organisation :
- subscribe in ClientApp to another topic to communicate with BackOffice, and communicate between EventChannel and ClientApp on another one (there is only presence for now and may be that’s all we need)
- communicate with a clients only through EventChannel: BackOffice ↔ EventChannel, and there is decision on EventChannel what to send to ClientApp ↔ ClientApp if need.
In future another app will need some communication features as well.
what makes more sense in your opinion? Are there any mistakes in my thought and is there any other options?