Is there a way to share LiveView "template" across users?

Watching ElixirConf 2019 - Beyond LiveView: Building Real-Time Features with Phoenix LiveView, PubSub and Presence by Sophie DeBenedetto started wondering is there a way to share LiveView element/template across users? e.g. presence list will be rendered N times once for each user etc.?

Can you expand on what this means for those of us who haven’t seen the talk?

By the description I’m assuming, at least conceptually, rendering a page fragment once and “broadcasting” it to multiple users - much like multiple users subscribed to the same channel receive the same messages; while the dispatch happens multiple times the formation of the payload only needs to happen once.

1 Like

Basically if you have a template that is rendering the same set of data for large number of users is there a way to have it run once and not once per user. E.g. in the demo each user in a room has a template that shows the presence list of users so if you had 1000 users in the chatroom you are redoing the same thing 1000 times.

You could build a process in between the liveview and the pubsub server, which does listen for those presence changes and then broadcasts the fully built html to subscribed live views.

1 Like

Prob a cool idea for a lib. if you could mark something as shared and just track who is supposed to be subscribed to the shared thing. (or even have it happen transparently)

One thing to keep in mind is that LiveView doesn’t do rerender as you might think of it like in React. It determines at compile time what the static and dynamic parts of a template are and then only pushes the purely dynamic values through the socket.

So doing this in a shared process probably wouldn’t have the impact you might expect.

Right but if the data fetch is backed by db 1000 fetches vs 1 could make a dif. Although it’s prb easier to optimize at data layer

That is simply poor solution architecture.

In a situation like that the event (message) consumed by the LiveView process should already contain all the aggregated data that is needed for the render. So the LiveView processes interested in that event should subscribe to a publisher process responsible for aggregating all the information.

2 Likes