rhcarvalho

rhcarvalho OP

A tip for my future self!

When testing LiveViews, it is often useful to test what happens on socket disconnection-reconnection. This cycle is what happens when a new deployment goes live, and when a browser tab wakes up from suspended state (which browsers do to save battery/resources).

There are so many ways to do it, but one neat trick is to crash the LiveView process by sending it an event it doesn’t handle. From the browser console:

liveSocket.js().push(liveSocket.main.el, "crash")

For completeness, you may also explicitly disconnect and connect the socket:

liveSocket.disconnect()
// and some time later...
liveSocket.connect()

And if you want to “drive it” from the server side, you can make the LiveView process exit. The tools available at an IEX shell include:

lvs = Phoenix.LiveView.Debug.list_liveviews()
# find the actual LiveView you want to target
lv = Enum.at(lvs, 0)
Process.exit(lv.pid, :kill)

And if you have LiveDashboard in the project, you can filter through the process list and click a button to kill the process, also causing the client to reconnect and spawn a new LiveView process.

Where Next? Top

Trending in Guides/Tuts Top

Other Trending Topics Top

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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement