Gathering detailed insights and metrics for live-view-model
Gathering detailed insights and metrics for live-view-model
Gathering detailed insights and metrics for live-view-model
Gathering detailed insights and metrics for live-view-model
npm install live-view-model
Typescript
Module System
Node Version
NPM Version
Elixir (51.74%)
TypeScript (47.35%)
JavaScript (0.91%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
5 Stars
100 Commits
1 Watchers
2 Branches
1 Contributors
Updated on Apr 29, 2025
Latest Version
0.3.1
Package Id
live-view-model@0.3.1
Unpacked Size
126.50 kB
Size
28.30 kB
File Count
149
NPM Version
10.9.0
Node Version
22.10.0
Published on
Nov 09, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
LiveViewModel is an Elixir library for building interactive web and mobile applications with a focus on real-time, event-driven architecture. It offers an alternative approach to state management and client-server communication, particularly suited for applications that require real-time updates and don't rely on server-side HTML rendering.
This project is currently under active development. The API and features are subject to change.
Use with caution in production environments.
LiveViewModel.Channel
: A behavior module for creating Phoenix channels that handle LiveViewModel logic.LiveViewModel.Encoder
: A protocol for customizing how data is encoded before being sent to clients.LiveViewModel.Event
: A struct representing events that can be sent from the server to clients.LiveViewModel.MessageBuilder
: A module for creating state change and patch messages.LiveConnection
: Manages the connection to the server and provides methods for joining channels and sending events.LiveViewModel
: A decorator and base class for creating view models that sync with the server state.@liveObservable
, @localObservable
, @action
, @computed
, @liveEvent
, @handleEvent
, @liveError
) for defining reactive properties and methods.LiveViewModel is particularly well-suited for:
Add LiveViewModel to your dependencies in mix.exs
:
1defp deps do 2 [ 3 {:live_view_model, "~> 0.3"} 4 ] 5end
Create a channel using LiveViewModel.Channel
:
1defmodule MyAppWeb.MyChannel do 2 use LiveViewModel.Channel, web_module: MyAppWeb 3 4 @impl true 5 def init(_channel, _payload, _socket) do 6 {:ok, %{count: 0}} 7 end 8 9 @impl true 10 def handle_event("update_count", %{"value" => value}, state) do 11 {:noreply, %{state | count: value}} 12 end 13end
Add the channel to your socket in lib/my_app_web/channels/user_socket.ex
:
1defmodule MyAppWeb.UserSocket do 2 use Phoenix.Socket 3 4 channel "room:*", MyAppWeb.MyChannel 5 6 # ... rest of the socket configuration 7end
Install the npm package:
1npm install live-view-model
Create a view model:
1import { 2 liveViewModel, 3 LiveConnection, 4 liveObservable, 5 liveEvent, 6} from "live-view-model"; 7 8@liveViewModel("room:lobby") 9class MyViewModel { 10 constructor(private conn: LiveConnection) {} 11 12 @liveObservable() 13 count: number = 0; 14 15 @liveEvent("update_count") 16 updateCount(value: number) { 17 return { value }; 18 } 19}
Connect and use the view model:
1import { connect, join } from "live-view-model"; 2 3const conn = connect("ws://localhost:4000/socket"); 4const viewModel = new MyViewModel(conn); 5join(viewModel); 6 7autorun(() => console.log("Count changed:", viewModel.count)); 8 9viewModel.updateCount(5); 10viewModel.updateCount(4);
@liveViewModel(topic: string)
Sets up a class as a live view model, connecting it to a specific Phoenix channel.
Usage:
1@liveViewModel("room:{roomId}") 2class LobbyViewModel { 3 // ... 4}
Functionality:
{}
placeholders (e.g., "room:{roomId}"
) that are replaced with the corresponding params
when join
ing the channel@liveObservable(serverKey?: string)
Marks a property for synchronization with the server and integrates with MobX to create observable properties.
Usage:
1@liveObservable("server_count") 2count: number = 0; 3 4@liveObservable.deep() 5messages: ChatMessage[] = [];
Functionality:
@liveObservable.ref
: Creates a reference observable@liveObservable.struct
: Creates a structural observable@liveObservable.deep
: Creates a deep observable@liveObservable.shallow
: Creates a shallow observable@localObservable()
Marks a property as a local observable, not synchronized with the server.
Usage:
1@localObservable() 2localCount: number = 0; 3 4@localObservable.ref() 5localReference: SomeType | null = null;
Functionality:
@localObservable.ref
: Creates a reference observable@localObservable.struct
: Creates a structural observable@localObservable.deep
: Creates a deep observable@localObservable.shallow
: Creates a shallow observable@liveEvent(eventName: string)
Defines a method that sends events to the server when called. Returns the payload to be sent to the server. Alternatively, you can use pushEvent(eventName, payload)
to send events manually.
Usage:
1@liveEvent("notify") 2notify(message: string) { 3 return { message }; 4}
Functionality:
@handleEvent(eventName: string)
Defines a method that handles events received from the server.
Usage:
1@handleEvent("navigate") 2handleMessage(payload: any) { 3 console.log("Navigating to:", payload.path); 4}
@liveError()
Specifies an error handler for the view model.
Usage:
1@liveError() 2handleError(error: any) { 3 console.error("View model error:", error); 4}
Functionality:
@action()
Alias for MobX action decorator.
Usage:
1@action() 2setCount(count: number) { 3 this.count = count; 4}
Functionality:
@computed()
Alias for MobX computed decorator.
Usage:
1@computed() 2get messageCount() { 3 return this.messages.length; 4}
Functionality:
LiveViewModel.Encoder
protocol to customize how data is serialized before being sent to clients.The library includes LiveViewModel.TestHelpers
module for writing tests for your LiveViewModel channels.
LiveViewModel integrates seamlessly with React using mobx-react-lite for efficient rendering and state management. Here's an example of how to use LiveViewModel in a React component:
1npm install live-view-model mobx mobx-react-lite react
1import React, { useMemo, useEffect } from 'react'; 2import { observer } from 'mobx-react-lite'; 3import { connect, join, leave } from 'live-view-model''; 4 5const App = () => { 6 const conn = useMemo(() => { 7 return connect('ws://localhost:4000/socket'); 8 }, []); 9 10 return ( 11 <LobbyComponent conn={conn} /> 12 ); 13} 14 15const LobbyComponent = observer(({ conn }) => { 16 const vm = useMemo(() => { 17 return new LobbyViewModel(conn); 18 }, [conn]); 19 20 useEffect(() => { 21 join(vm); 22 return () => leave(vm); 23 }, [vm]); 24 25 return ( 26 <div> 27 <h1>Lobby</h1> 28 <p>Count: {viewModel.count}</p> 29 <button onClick={() => viewModel.increment()}>Increment</button> 30 <button onClick={() => viewModel.decrement()}>Decrement</button> 31 </div> 32 ); 33}); 34 35export default App;
While LiveViewModel shares similar goals with Phoenix LiveView, it takes a different approach:
This distinction allows LiveViewModel to be used in scenarios where full server-side rendering is not possible or desirable, such as in native mobile applications.
Contributions are welcome! Please feel free to submit a Pull Request.
No vulnerabilities found.
No security vulnerabilities found.