How LiveState works
mainLiveState is a state-synchronization framework where the server acts as the single source of truth.
The Workflow:
- Events: Clients dispatch events to the server.
- Handling: The server handles these events in Elixir and updates the application state.
- Updates: The server pushes state updates back to the client. Subsequent updates are diffed using the
jsondifflibrary and sent injsonpatchformat to minimize payload size.
Comparison with LiveView: While LiveView manages both server logic and view presentation in Elixir, LiveState separates them. LiveState event handlers are written in Elixir, but the client is responsible for rendering the state and dispatching events. This makes LiveState better suited for applications where the client-side rendering logic needs to be decoupled from the server or used in non-Phoenix environments.