LiveReactNative.MobileChannel (live_react_native v0.0.3)

View Source

Phoenix Channel that bridges React Native clients to Phoenix LiveView business logic.

Each joined channel resolves the requested LiveView from the router, runs its on_mount hooks, and drives its callbacks (mount/3, handle_event/3, …) on a persisted %Phoenix.LiveView.Socket{} — the "channel-as-process" model. Assigns and side-effect commands stream back to the client as JSON; no HTML is rendered.

Topics

React Native client (MobileClientProvider + useChannel)
        Phoenix Channel, topic "mobile:<path>"
LiveReactNative.MobileChannel   (holds the persisted LiveView socket)
        direct callback invocation
your LiveView (mount/handle_event/handle_info)  unchanged

Clients join topics of the form mobile:<path> (e.g. mobile:/mobile/counter), which map to the LiveView routed at <path>.

Authentication

Auth is handled at the socket level by a LiveReactNative.MobileSocket behaviour implementation (resolve_user/2 turns the connect params — e.g. a token — into a user; build_session/3 builds the session map). The resolved session is then gated by the router's on_mount hooks when this channel joins, so the same hooks protect web and mobile. See the docs/ architecture and auth guides.

Summary

Functions

Combines mount params, path params, and query params with proper precedence. Path params override mount params (Phoenix Router behavior).

Execute on_mount hooks extracted from live_session - this is CRITICAL for security!

Extracts path parameters from route patterns, exactly like Phoenix Router.

Extracts path parameters from Phoenix Router route_info and actual path.

Extract a navigation intent from a LiveView socket after an event, or nil.

Alias for handle_params_navigation/3 - for test compatibility.

Handles navigation with handle_params/3 (for parameter-only changes). Used when navigating within the same LiveView with new parameters.

Alias for extract_path_parameters/2 - for test compatibility.

Mounts a LiveView for the URI the client last had (reconnect restore).

Alias for mount_with_params/5 but with different signature for test compatibility.

Mounts LiveView with handle_params/3 support (like web LiveView does).

Parses URL parameters from a mobile path, splitting path and query string.

Build the channel message {event, payload} for a post-event navigation, or nil. Internal testing seam — not part of the stable public API.

Extracts the client-sent current URI from join params (for reconnect restore).

Functions

child_spec(init_arg)

combine_all_params(mount_params, path_params, query_params)

Combines mount params, path params, and query params with proper precedence. Path params override mount params (Phoenix Router behavior).

compute_diff(module, event, payload, live_socket)

execute_on_mount_hooks(session_info, socket, params)

Execute on_mount hooks extracted from live_session - this is CRITICAL for security!

This ensures mobile clients go through the SAME authentication pipeline as web clients, respecting live_session boundaries and on_mount hook requirements.

extract_path_parameters(actual_path, route_pattern)

Extracts path parameters from route patterns, exactly like Phoenix Router.

Examples: extract_path_parameters("/mobile/users/123", "/mobile/users/:id") # => %{"id" => "123"}

extract_path_parameters("/mobile/api/v2/pages/100", "/mobile/api/v:version/pages/:id") # => %{"version" => "2", "id" => "100"}

extract_path_params_from_route_info(route_info, actual_path)

Extracts path parameters from Phoenix Router route_info and actual path.

extract_redirect(socket)

@spec extract_redirect(Phoenix.LiveView.Socket.t()) ::
  {:navigate, String.t()} | {:redirect, String.t()} | nil

Extract a navigation intent from a LiveView socket after an event, or nil.

Only INTERNAL navigation (push_navigate/redirect with a :to path) is surfaced to the mobile client. External redirects (redirect(external: ...)) are intentionally out of scope — a native app shell has no in-app analog — and return nil. Internal testing seam; not part of the stable public API.

handle_in(event, payload, socket)

Callback implementation for Phoenix.Channel.handle_in/3.

handle_info(msg, socket)

Callback implementation for Phoenix.Channel.handle_info/2.

handle_live_view_event(event_name, payload, socket)

handle_live_view_info(msg, socket)

handle_navigation_with_params(new_path, params, socket)

Alias for handle_params_navigation/3 - for test compatibility.

handle_params_navigation(url_params, uri, socket)

Handles navigation with handle_params/3 (for parameter-only changes). Used when navigating within the same LiveView with new parameters.

join(topic, params, socket)

Callback implementation for Phoenix.Channel.join/3.

match_route_pattern(actual_path, route_pattern)

Alias for extract_path_parameters/2 - for test compatibility.

mount_for_uri(live_view_module, params, session \\ %{})

Mounts a LiveView for the URI the client last had (reconnect restore).

Parses the "uri" join param's query string, then runs mount/3 followed by handle_params/3 (when implemented) so a reconnect lands on the same filter/page/params the user was on — not the initial mount. Returns the resulting assigns map.

mount_live_view_with_params(live_view_module, mount_params, path, socket)

Alias for mount_with_params/5 but with different signature for test compatibility.

mount_with_params(live_view_module, mount_params, url_params, uri, socket)

Mounts LiveView with handle_params/3 support (like web LiveView does).

Calls both mount/3 AND handle_params/3 in sequence:

  1. mount/3 with mount params (device_id, user_id, etc.)
  2. handle_params/3 with URL params (query + path parameters)

parse_url_parameters(path)

Parses URL parameters from a mobile path, splitting path and query string.

Examples: parse_url_parameters("/mobile/posts?category=tech&page=2") # => {"/mobile/posts", %{"category" => "tech", "page" => "2"}}

parse_url_parameters("/mobile/counter") # => {"/mobile/counter", %{}}

redirect_message(live_socket)

@spec redirect_message(Phoenix.LiveView.Socket.t()) :: {String.t(), map()} | nil

Build the channel message {event, payload} for a post-event navigation, or nil. Internal testing seam — not part of the stable public API.

resolve_live_view_module(live_path, params)

resolve_live_view_module_with_route(live_path, params)

start_link(triplet)

terminate(reason, socket)

Callback implementation for Phoenix.Channel.terminate/2.

uri_from_params(params)

Extracts the client-sent current URI from join params (for reconnect restore).

Returns nil when the client did not send one.