Liveview Promo
Liveview Promo
Liveview Promo
Bruce Tate
All rights reserved. No part of this publication may be reproduced, stored in a retrieval system,
or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording,
or otherwise, without the prior consent of the publisher.
ISBN-13: pending
Encoded using the finest acid-free high-entropy binary digits.
Book version: B1.0—Monthname, yyyy
Contents
Change History . . . . . . . . . . . . v
2. LiveView Events . . . . . . . . . . . . 19
The LiveView Event Infrastructure 19
Buttons and Keystrokes 21
Forms and Schemaless Changesets 26
Events We Didn’t Cover 36
Your Turn 37
In this book, we’ll look at how LiveView works in broad strokes, and then we’ll
examine how data moves between the client and server. This book will be
heavily supplemented by the videos on Groxio. We’ll use this book to explore
the theory. We’ll explore high-level concepts, with a few simple examples.
You’ll see how Phoenix generates code, learn strategies for designing your
own code, and find out how to incorporate many different kinds of events.
In the videos, we’ll explore detailed examples. You’ll find out how to do common
tasks. We’ll start with the most basic applications so you can work with
LiveView without having to focus too much on business logic. We’ll integrate
change validation through Ecto changesets without requiring a database.
We’ll graduate to a more complex application for memorizing things.
In both the book and videos, we’ll organize our code into layers so you can
address only a small bit of complexity at any given time. As you read now,
we’re going to stay at a high level. We’ll walk through a basic LiveView pro-
gram, starting at mix phx.new and going all the way through trying our working
application in the development environment.
Let’s get started. In order to best understand the problem LiveView solves,
it’s best to understand a bit of history. The first step in understanding LiveView
is reviewing how web programming was traditionally done.
These frameworks all focus on functions. A request comes in, goes through
some sort of process, and a response flows out. Even within object oriented
frameworks, requests are functions, with requests and responses.
1. https://wiki.c2.com/?ModelViewControllerHistory
Controller
Model
View
Over time, programmers modified MVC to work on the web with a request-
response flow. These developers (from many different backgrounds) eventually
settled on a new name and programming model called MVC2 or model-2. The
following figure shows how it all works.
Request Controller
Browser Model
Response View
A user makes a request. Then, a router layer receives that request and routes
it to a controller. The controller fetches external data, perhaps from a database,
from the model layer, and then passes to a view. The view layer will use the
state and perhaps a template to render the data.
This programming model was tremendously popular, and exploded with the
growth of the Java programming language. Model-2 is still broadly used today
in many Java frameworks, Ruby on Rails, and even Elixir’s Phoenix.
Mail. Twitter and Facebook shared live timelines that updated without user
interaction. MVC2 was too limited to deliver those new applications. Over
time, frameworks sought ways to build APIs to make partial requests to the
server where each request changes.
?
Request
Controller
?
?
Browser Model
? View
Response
?
The question marks in this figure show individual web requests that all service
the same individual page. Each one requires its own MVC2 design. Many
different frameworks in several different languages attempted to solve this
problem. Building interactive applications became difficult, almost unman-
ageable. Dozens, even hundreds, of JavaScript frameworks emerged to weave
order out of this chaos. Unfortunately, the management of these JavaScript
libraries became a problem of its own, and each web project became an
increasingly distributed mess.
This is where we are today. MVC2, a framework that was never built to handle
tiny requests and responses, is breaking under the weight of the interactive
single-page app. Many tiny interactive requests from the same page require
broader skills across multiple languages. Something has to give.
Enter LiveView
Phoenix LiveView is a whole new programming model built from the ground
up to handle single-page flows. Rather than building functions satisfying
independent requests, LiveView is centered around state. Developers work
in two distinct dimensions. They render the state with a function. Then, other
programs—including the browser—can change the state with events.
In this series, we’re going to work through some basic applications, and then
ramp up the complexity slowly over time. Throughout this book when we refer
to the LiveView library, we’ll use the camel-case word LiveView. When we’re
talking about an interactive view built with this library, we’ll use two lower
case words live view.
Let’s talk about how it works. LiveView assumes the burden of the layers
between the client and server, including the layer of JavaScript that runs on
the browser. With LiveView taking the responsibility of distributed requests
between the JavaScript client and Elixir server, programming gets much
simpler. The following figure tells the story.
Initial LiveView
Request
Set Data
Browser Response Render Data Context Model
Change Data
Interactive
Requests
Once the live view renders the initial request, it listens for events. Rather
than these events triggering a render, the events trigger a change in state.
Then, each state change triggers a render. Did you get the difference between
live views and MVC2? It all depends on clearly defining the responsibilities
of rendering and state change.
In the sections that follow, we’ll talk about how these three concepts work.
We will write very little code, but you’ll immediately see the interactive nature
of what’s happening. We’ll start with the initial request to a LiveView.
[liveview] ➔ cd dazzle
[dazzle] ➔ mix ecto.create
The database for Dazzle.Repo has been created
...
2. https://hexdocs.pm/phoenix/1.7.0-rc.3/installation.html
Rebuilding...
Done in 154ms.
...
We’ve shortened these listings, but you get the idea. If all goes well, you can
point your browser to http://localhost:4000 and see the familiar Phoenix startup
screen. If you get an error, don’t panic. While LiveView is pretty young, the
community is vibrant. Just paste a chunk of your error into Google, and the
excellent LiveView community will point you to the source of the problem and
potential solutions.
You might not be able to tell right now, but your installation has everything
you need to create live views. Let’s talk about some of configuration that sets
up a live view. When you pointed your browser to http://localhost:4000, you
started a cascade of functions called plugs. Though this initial page is not a
live view, the initial set of functions remains the same.
Don’t worry. Plugs are not hidden within many layers of framework code.
They’re all explicit. You can see every line of code that Elixir touched.
Elements of a LiveView
As you might imagine, the LiveView programming model is simple. You’ll need
to consider these steps: