1 of 51

LiveView keeps you warm!

aka: why you should consider LiveView for your next Web project

Arjan Scherpenisse

�Erlang, Elixir & Friends Devroom, FOSDEM'23

2 of 51

Agenda

  • Background
    • About me
    • Knitting machine hardware / software
  • LiveView
    • How LiveView works (a history lesson)
    • Code reusability (scaling, componentization)
    • Javascript integration
  • Wrapping up

3 of 51

Background

4 of 51

About me

Programming since I was a kid, first OTP R13B01

Scientific background with a hands-on attitude

Interested in AI, hardware/software interaction, art…

5 of 51

About this art stuff:

Working together with Klasien van de Zandschulp for several projects

6 of 51

7 of 51

8 of 51

9 of 51

10 of 51

“It is like a printer, but for yarn”

(famous last words)

11 of 51

Learning to knit

Learned to operate the machine after several weeks

Knitted several garments (hats mostly)

Sourcing the proper yarn is hard, process error-prone

12 of 51

This is me knitting

13 of 51

Automating the knitting

Found Hackspace Bamberg which already had hacked a Passap machine

Used same schematic for arduino to control the knitting

14 of 51

Old UI

The UI am I replacing:

15 of 51

New UI

16 of 51

Knitting machine software diagram

Arduino

LiveKnit.Serial

LiveKnit.Control

%LiveKnit.Machine.Passap{}

USB

Phoenix �PubSub

GenServer�state

LiveKnit.SerialMonitor

LiveKnitWeb.Live.Main

Phoenix �PubSub

Browser

HTML�WebSocket

Here is where the LiveView magic happens :-)

Hardware

Elixir / OTP

Browser

Nerves.UART

17 of 51

What is LiveView?

18 of 51

“LiveView provides rich, real-time user experiences with server-rendered HTML.”

19 of 51

A little bit of �web history

20 of 51

First there was the “static HTML” web (1991)

Browser

index.html

HTML

21 of 51

The PHP / DHTML wave (~2000)

Browser

index.php

HTML

Everything was rendered on the server

Javascript was added for small bits of interactivity

Server state managed with sessions/cookies

logic

logic

22 of 51

Javascript Wave (from ~2008)

More logic on the client, separation of backend and frontend developers

Culminating in the rise of the SPA (Single Page Application), React, …

Browser

<html>

HTML

REST / GraphQL

“Ajax”

logic

logic

23 of 51

Phoenix LiveView (from 2018)

Browser

LiveView

HTML�WebSocket

A mix of client and server development like in the Old Days™, no need to write anymore Javascript! (unless you really want to)

logic

24 of 51

So how does it actually work?

25 of 51

Glad you asked…

1 liveview process per page, that contains all state,

Templates are rendered on server

DOM diffs are sent to client

Events are sent back to server

26 of 51

Browser

LiveView process (connected)

DOM diffs

state

LiveView render (not connected)

Browser

HTML

WebSocket (Phoenix Channel) is established…

Events

27 of 51

And this is what that looks like:

Liveview hello world

▶ mix phx.new live_demo --no-ecto --no-gettext --no-dashboard --no-mailer

28 of 51

Let’s make the knitting live :-)

29 of 51

30 of 51

LiveView is live & reactive

handle_event callback lets you react to events from the browser

handle_info callback lets you react to events from other parts of the application

(for instance with Phoenix PubSub)

31 of 51

LiveKnitWeb.Live.Main

Let’s look at the knitting machine code

* disclaimer!

32 of 51

Patterns of reusability

33 of 51

Patterns of reusability

  • Function components (stateless)
  • Phoenix.LiveComponent (stateful)

34 of 51

Let’s view the LiveKnit components

These are all components

35 of 51

Function components

36 of 51

LiveComponent (stateful)

37 of 51

LiveComponent

�<live_component id="LC1" module={MyComponent} />

Browser

LiveView process (connected)

DOM diffs

Gathered for all components

LiveView state

Addressed events�(phx-target)

LC2 state

LC1 state

render

38 of 51

Slots and attrs

  • Deal with inner content of components
  • Provide compile-time validation
  • Comparable to Web Components

39 of 51

Other component options

40 of 51

41 of 51

Javascript integration

42 of 51

Javascript integration

…Because sometimes, you must.

  • Hooks,
  • push_event()
  • Phoenix.LiveView.JS

43 of 51

LiveView Javascript hooks

Browser

LiveView process (connected)

push_event(“...”, payload)

LiveView state

this.pushEvent(...)

Hook JS

Hook state

44 of 51

LiveKnit analyze page demo

45 of 51

LiveView “cons”

  • LV totally depends on reliable client ←→ server comm
    • No “offline mode” to speak of
    • Latency can be a killer
    • Optimistic UI updating…���
  • Frontend developers having to learn Elixir?

<button type="submit" phx-disable-with="Saving...">Save</button>

46 of 51

The Future

47 of 51

LiveView competitors

Any popular tech gets cloned into other programming languages:

  • Laravel LiveWire (PHP)
  • HotWire (Ruby/Rails)
  • … and may more

https://github.com/liveviews/liveviews

Rise of WASM causes other languages to be used in the browser

48 of 51

LiveView native

Native wrapper

iOS / Android

LiveView process

“UI view tree” diffs

state

Events

LV bridge library

Mobile client

Server

49 of 51

LiveView native

Native wrapper

iOS / Android

Linked BEAM / Lumen

LiveView process

state

LV bridge library

Or even…!

50 of 51

Thanks for listening!

…any questions?

51 of 51

Bonus: The “Pat” DSL

font = Font.load(:sigi5b, fg: "X", bg: " ", stride: 1)

string = "hello there"

{w, h} = Font.measure(font, string)

target = Pat.new(w, h, " ")

target = Font.render(font, target, string, 0, 0)

Pat.print(target)