James_E

James_E

Whenever there’s a connection error on LiveView, the current version of phx.new sticks a completely fake progress bar at the top of the screen, which fills at the hardcoded rate of 5% of the remaining distance to 95%, each frame, without regard to any actual reconnection progress or post-reconnection sync progress.

I’d like to replace this with a less misleading disconnection indicator, such as an indeterminate progress bar. But I’d also like to (in the event that the latter takes more than 1 frame) switch over to showing a “real” progress bar showing the approximate completion of the actual post-reconnection sync.

So: does Phoenix have any hooks to tell the difference between these? I tried to find it myself, but I couldn’t spot where the existing “page-loading” hooks are coded, so I wasn’t immediately able to just look around at the neighboring code to check.

(I’m not asking about how to program the replacement topbar drawing code as that’s going to be a pretty simple exercise and is entirely out-of-scope of Elixir/Phoenix anyway; I’m only asking how I would hook into LiveView to know when to run which version of the drawing code.)

Showing Posts 1 to 3

Hermanverschooten

Hermanverschooten

a grep shows them in phoenix_live_view/assets/js/phoenix_live_view/live_socket.js around line 763.

gushonorato

gushonorato

Have you tried to implement a disconnection feedback using lifecycle events?

<div id="status" class="hidden" phx-disconnected={JS.show()} phx-connected={JS.hide()}>
  Attempting to reconnect...
</div>

You can find more information here: Bindings — Phoenix LiveView v1.2.5*

James_E

James_E OP

oh, wow, guess I was looking in the entirely wrong source tree. Thanks for the pointer.

Though it looks like that file just contains a wrapper function… a bit more digging didn’t reveal any obvious hooks into the larger state.

I see, thank you! :eyes: I didn’t realize Phoenix really does have a separate set of events for that.

I’m still not sure how to quantify a LiveView resync (for example, is there any interface to check the size of the socket’s queue during reconnect), but this at least allows my app to react differently to a mere loading event that happens to be taking a while vs. a clear disconnect from the LiveView.

/* app.js */
{
  // Show progress bar on live navigation and form submits
  // https://forum.elixirforum.com/t/phx-error-topbar-canvas-tell-the-difference-between-loading-and-connecting/67898?u=james_e
  topbar.config({
    barColors: {0: "#29d", "0.5": "orange", "1.0": "#29d"},
    shadowColor: "rgba(0, 0, 0, .3)",
    autoRun: "indeterminate" // https://github.com/buunguyen/topbar/issues/21
  })
  let is_loading = false, is_connected = true;
  window.addEventListener("phx:page-loading-start", _info => {
    is_loading = true;
    topbar.show(300); // TODO make this actually show reconnection, resync, or loading progress
  })
  window.addEventListener("phx:page-loading-stop", _info => {
    is_loading = false;
    if (is_connected)
      topbar.hide()
  })
  window.addEventListener("phx:x-liveview-disconnected", _info => {
    is_connected = false;
    topbar.show()
  })
  window.addEventListener("phx:x-liveview-connected", _info => {
    is_connected = true;
    if (!is_loading)
      topbar.hide();
  })
}
<%!-- home.html.heex -->
<main
  phx-disconnected={JS.dispatch("phx:x-liveview-disconnected")}
  phx-connected={JS.dispatch("phx:x-liveview-connected")}
>
  …
</main>
— All posts loaded —

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
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
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New

Other Trending Topics Top

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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews