I’m adding the new phoenix 1.7 liveview 0.18.16 stream to my app, to show live logs.
This logs list can be quite long as intented, but I would like to set a limit, say to a thousand logs displayed, and deleting the oldest ones from the dom while new logs are pouring in.
Before stream, I was simply using Enum.take(1000), but with the stream functions, I don’t know how to remove oldest logs (apart from using a liveview hook, but… it seems not in line with the goal of liveview)
|> #check to see how many you’ve streamed and if it is over 1000, start stream_delete the oldest item.
Sure, but then you have to keep track server side of all the dom ids (at least) currently rendered client side. The rendering would be more performant, but it’s more or less like a good old assign (but more complicated), am I right?
I think this part [ delete_log| _ ] = @streams.logs is a bit flaky, since the all point is to not have the rendered logs stored server side after being sent to the client. So I expect stream.logs to not have records of the logs sent to the client a while ago.
Keeping track of the count of sent logs could be a nice track though, but we would need a function stream_delete that can take an offset of some sort instead of a specific element.
I think with this problem it’s good to keep in mind where the state lives.
In traditional assigns, state lives on the server (list of logs in assigns).
In stream assigns, state lives in the client browser (in the form of the list of DOM nodes with ids).
So you will need to either implement JS hooks or track state server-side (DOM ids).
One way would be to update Phoenix stream API so it accepts options such as max stream length (e.g. stream(socket, :logs, logs) ← make it accept additional options). But that would be on Phoenix devs.