webdeb

webdeb

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)

Showing Posts 16 to 7

Miserlou

Miserlou

I’ll throw my hat into the ring too, I guess.

Just pushed an alpha of this:

https://github.com/Miserlou/live_json

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

Then, in your JS console:

window.viz_data
// {1: ["a"], 2: ["b"] ... 99999: ["zzzzz"]}

Only deltas are sent over the wire, so it’s quite fast!

More details soon, but works for me.

webdeb

webdeb OP

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.

The code is open source, and very basic livestate.io · GitHub

@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 :wink:

justinmcp

justinmcp

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.

harmon25

harmon25

That is awesome!

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…

justinmcp

justinmcp

Looks like I am pursing a similar idea over at GitHub - justinmcp/taper: Taper is a React (with SSR) and server-side-redux-like environment for Elixir+Phoenix. · GitHub. 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 GitHub - justinmcp/taper-examples: Examples for the Taper project · GitHub.

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.

Shaping up nice though.

methyl

methyl

@harmon25 @webdeb I started a channel livedata on Elixir Slack, let’s connect there and kick it off :slight_smile:

harmon25

harmon25

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!

webdeb

webdeb OP

Cool! I think this is what I had in mind!

Update

@methyl
After watching your screencast. I am sure it’s exactly what I had in mind :slight_smile:

methyl

methyl

Hey there.

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: GitHub - surferseo/livedata_todomvc · GitHub which is a proof of concept for the idea.

While the POC implementation is hideous, the API is really clean and powerful - check out livedata_todomvc/lib/phoenix_livedata_todomvc_web/live_data/todo_state.ex at master · surferseo/livedata_todomvc · GitHub and livedata_todomvc/assets/js/containers/TodoList.js at master · surferseo/livedata_todomvc · GitHub.

If you are interested in hearing more, check out this video https://www.youtube.com/watch?v=fvNy9bh8_vs where I talk a bit about motivations and the ugly internals.

Trying to make it play well with LiveView makes a lot of sense, as it lowers the barrier of entry to integrate it with existing JS codebases.

lud

lud

To me it looks like this library has no dependencies at all (besides the test lib for dev).

https://github.com/ElixiumNetwork/bert-elixir/blob/master/package.json

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews