Understanding the Phoenix.LiveView Behaviour with an Example

Luca Berton
4 min readJul 9, 2024

Introduction

Phoenix.LiveView is a powerful tool in the Phoenix framework that allows you to build rich, interactive web applications with real-time features. It simplifies the development process by handling both the client and server-side logic seamlessly, maintaining a stateful connection that updates the UI as needed. This article explores the core concepts of Phoenix.LiveView, its life-cycle, and how to perform asynchronous operations with an example.

Life-cycle of a LiveView

A LiveView starts as a regular HTTP request, rendering an HTML response. Upon the client’s connection, it upgrades to a stateful view, ensuring that even if JavaScript is disabled, a static HTML page is served. Here’s a breakdown of the lifecycle:

  1. Initial Request: The LiveView is rendered via a regular HTTP request.
  2. Mounting: The mount/3 callback initializes the LiveView with parameters, session, and socket state.
  3. Rendering: The render/1 function generates the HTML content.
  4. Client Connection: When the client connects, the LiveView process is spawned on the server, re-invoking mount/3 and handle_params/3.
  5. State Updates: Any state changes trigger a re-render, pushing…

--

--

Luca Berton

I help creative Automation DevOps, Cloud Engineer, System Administrator, and IT Professional to succeed with Ansible Technology to automate more things everyday