# Phoenix LiveView Real\-Time Updates

How Phoenix LiveView keeps every open browser in step: press \+ on one screen and watch the event, the PubSub broadcast and the tiny diffs update every connected client

By Marco Cantu

Topic: Computing, AI &amp; Electronics

[Open the interactive Jig](https://ask.jig.com/j/f7QzjFA2WvKQ)

## Transcript

### 1. One page, three screens

Three people have the same thermostat page open: two laptops and a tablet\. Phoenix LiveView keeps their pages in step\. When anyone changes the number, every screen shows it at once, with no reloading\.

Phoenix is a web framework for the Elixir language\. LiveView is the part that makes pages live\.

### 2. First visit: a plain page

When a browser first asks for the page, the server answers with ordinary HTML, the text that describes a web page\. The screen fills in quickly, even before any live features start\.

This first answer also helps search engines and slow phones\.

### 3. Opening a live line

Next, a little JavaScript in the page opens a connection that stays open\. On the server, Phoenix starts a LiveView process for that browser: a tiny helper that remembers the page's state, here the number 70\.

The setup code, called mount, runs twice: once for the plain page and again when the live line connects\.

### 4. Tuning in to one channel

Each process subscribes to a shared topic with Phoenix PubSub, short for publish and subscribe\. Think of it as a radio channel: anything said on it reaches every process that tuned in\.

### 5. Press \+ on one screen

Press the \+ button on the left laptop\. The browser does not change the number itself\. It sends a small event up its live line, and the server code called handle\_event decides what happens next\.

Yellow parcels are events going up\. Green chips are updates coming back down\.

### 6. The server tells everyone

The process handling the click broadcasts the new number on the PubSub channel\. Every subscribed process receives the message in handle\_info and updates its memory from 70 to 71\.

One broadcast reaches every subscriber, even on other servers in a cluster\.

### 7. Only the change travels

Each process works out what changed on its page and sends just that difference, called a diff, down its line\. The page swaps in the new digit, and all three screens now read 71\.

Only the 1 that changed travels\. The 7 that stayed the same is not sent again\.

### 8. Your turn: press any \+

Press \+ on any laptop or the tablet and follow the round trip: event up, broadcast across, diffs down\. Whoever presses, everyone sees the same number\.

Drag the empty table to look around, even from behind the server\.
