Hi, we have an NextJS Frontend Application which is basically React.
Now we would like to use a Phoenix App as a Service for some Live data. But we only need the JSON no HTML. Is Phoenix LiveView a good fit for this, or should we go with the low-level Phoenix Channels.
In comparison, I think that Channels is really low-level. On the other hand LiveView is very high-level and too HTML specific. IMHO something like LiveState would be a cool thing.
For example const { state, dispatch } = useLiveState() (React hooks)
I would go with channels, yes. I don’t know if they are low level (I would say websockets are rather low-level, but not channels), but they are very easy to use, so low level or not they are a great tool if you want to push/stream data from the backend.
Channels could work. Have you considered using BERT instead of JSON? Sending data on the Elixir side would use :erlang.binary_to_term/1, which is fast. Then you could use Bert.js on the front end to decode into JS objects. Although it is admittedly a pretty dependency heavy lib.
My main concern is about the features that LiveView is offering besides HTML sync and event passing (which for my use-case aren’t important) but it seems that there are also other important features on top of plain Channels.
I actually think it’s an idea with a lot of potential and use-cases that could be supported either in LiveView or as a separate library.
Being able to use whole React ecosystem while keeping the state in sync with your GenServers (or other abstraction) is something that we already experimented with. Take a look at this repo: https://github.com/surferseo/livedata_todomvc which is a proof of concept for the idea.
This looks is amazing, I would love to help polish LiveData and get it published on hex!
Exactly the kind of middle ground I am looking for - a lower level state abstraction than LiveView, but something a bit higher level than just channels.
I have had the exact mental model of redux <-phoenix-channel-> gen_server - and even made a similar proof of concept that leverages the gen_server state as the source of truth.
The addition of fast-json-patch is a great idea and is what morphdom does but with just the state, genius!
Any progress since the video and the code that you have posted?
If you started breaking it out into a new repo, I would be jumping on to contribute right away!
Looks like I am pursing a similar idea over at https://github.com/justinmcp/taper. You can render react components directly from a template (.html.jsx) and they can connect to a server-side version of redux (basically the same API as redux). It can also render jsx server side only, skipping hydration on the client. There is currently one example at https://github.com/justinmcp/taper-examples.
Still really early days. I want to put back a “remote call” hook that could call out to any gen_server, handle store loading/persistence in a reasonable way, etc, etc.
Looks like a lot of the same thoughts we are pursuing. Join us on the #livedata slack group if you haven’t already, and we can collab on these ideas!
Thinking there is room for both a lower level API that could be dropped into a phoenix app for server-side state in your JS client, and something that integrates them together more, for a fullstack next.js(ish) Elixir framework - which does server-side react rendering, and wires it all up - which is what taper is shaping up to be…
One thing we have implemented is sending json-diffs, akin to live_view diffs. Taper is just sending the entire state each time from the looks of it. Think there is room for some testing to determine if the json diffing actually helps at scale. I could imagine if the json is not very big - the diff might actually be more bytes than just sending the full json. It also of course comes at a cost of calculating the diff, also on the client applying it…
Thinking there is room for both a lower level API hat could be dropped into a phoenix app for server-side state in your JS client
One thing we have implemented is sending json-diffs, akin to live_view diffs. Taper is just sending the entire state each time from the looks of it.
100%. Sounds like a good idea. I have vaguely worked out having the reducers return a ecto.changeset instead of the entire new state, and then sending the changeset across the wire… I’m still not 100% on the idea though, maybe just a json-diff thing is better as you say.
I’ll pop into the channel and we can chat about it all.
Guys, I started working my initial state as a service idea, which should be a simple way to share state. I call it livestate (livestate.io) And I would love you to check it.
@methyl I liked your solution for special purpose state very much, as it made possible to use type generation for TS, and custom methods. And it was actually your ideas which inspired me to go with the json-patch method
Built for my own needs, but maybe it’ll be handy for you as well.
It’s very simple and works with existing LiveViews - just use LiveJson.push_patch the way you’d use assign or push_event. It has two modes, jsondiff and rfc mode, which use Jsondiff and JSON-Patch, respectively. jsondiff mode is very fast, and rfc mode is compatible everywhere.
As an example you can:
def handle_info({:new_data_to_visualize, new_data} = _event, socket) do
{:noreply,
socket
|> push_patch("viz_data", new_data)
}
end