Phoenix in Action
Geoffrey Lessel
Foreword by Saša Jurić
  • April 2019
  • ISBN 9781617295041
  • 336 pages
  • printed in black & white

This book will teach you the mechanics of the technology but, more importantly, it will explain how to think in Phoenix.

From the Foreword by Saša Jurić, author of Elixir in Action, Second Edition

Phoenix is a modern web framework built for the Elixir programming language. Elegant, fault-tolerant, and performant, Phoenix is as easy to use as Rails and as rock-solid as Elixir's Erlang-based foundation. Phoenix in Action builds on your existing web dev skills, teaching you the unique benefits of Phoenix along with just enough Elixir to get the job done.

About the Technology

Modern web applications need to be efficient to develop, lightning fast, and unfailingly reliable. Phoenix, a web framework for the Elixir programming language, delivers on all counts. Elegant and intuitive, Phoenix radically simplifies the dev process. Built for concurrency, Phoenix channels make short work of developing real-time applications. And as for reliability, Phoenix apps run on the battle-tested Erlang VM, so they’re rock solid!

About the book

Phoenix in Action is an example-based book that teaches you to build production-quality web apps. You’ll handle business logic, database interactions, and app designs as you progressively create an online auction site. As you go, you’ll build everything from the core components to the real-time user interactions where Phoenix really shines.

Table of Contents detailed table of contents

Part 1: Getting started

1 Ride the Phoenix

1.1 What is Phoenix?

1.2 Elixir and Phoenix vs. the alternatives

1.2.1 Real-time communication

1.2.2 Efficient, small processes

1.2.3 Background computation

1.2.4 Minimal hardware requirements, low-cost scaling

1.2.5 It’s not all roses

1.3 The power of Elixir

1.3.1 Scalability

1.3.2 Supervision trees

1.3.3 Erlang VM

1.3.4 Macro and metaprogramming support

1.3.5 OTP

1.4 Functional vs. object-oriented programming

1.4.1 Overview of functional programming

1.5 Keep reading


2 Intro to Elixir

2.1 The basics

2.1.1 IEx

2.1.2 Basic types

2.1.3 A small detour into functions

2.1.4 Data type details

2.1.5 Back to modules and named functions

2.1.6 alias

2.1.7 import

2.2 Other idiomatic Elixir language features

2.2.1 The pipe operator

2.2.2 Pattern matching

2.2.3 Using IEx.Helpers.v/1


3 A little Phoenix overview

3.1 Follow the data

3.1.1 The basics of a web request

3.1.2 Endpoint

3.1.3 Router

3.1.4 Controller


3.1.6 Templates

3.2 Putting it all together


Part 2: Diving in deep

4 Phoenix is not your application

4.1 I thought this book was about Phoenix

4.2 The first steps in building your application

4.2.1 Defining an item

4.2.2 Adding a fake database

4.2.3 Getting an Item by id

4.2.4 Getting an Item based on other information

4.3 Next steps


5 Elixir application structure

5.1 Moving from a single file to an application

5.1.1 Using Mix to create a new application

5.1.2 Generating an auction umbrella application

5.1.3 The magic mix.exs file

5.2 Organizing, compiling, and running your new application

5.2.1 Breaking apart the three modules

5.2.2 Compile and run!

5.2.3 Running the Auction application for the first time

5.3 Using hex to get external dependencies

5.3.1 Pulling in your dependencies


6 Bring in Phoenix

6.1 Installing Phoenix on your system

6.2 Creating a new Phoenix application

6.2.1 Running your server for the first time

6.3 Listing items from the fake repo

6.3.1 Modifying the controller and template


7 Being persistent with a database

7.1 A quick intro to Ecto

7.2 Configuring Ecto

7.2.1 Using Ecto’s Mix tools to set up your database

7.3 Preparing Auction to use the database

7.3.1 Defining the Auction.Item schema

7.3.2 Migrating the database

7.3.3 Pointing the public interface to the new repo

7.3.4 Instructing the application to supervise the database connection

7.3.5 Testing it out

7.4 Creating, retrieving, and deleting data in the database

7.4.1 Inserting data

7.4.2 Retrieving data

7.4.3 Deleting data

7.4.4 Updating data

7.4.5 What about the website?


8 Making changes with Ecto.Changeset

8.1 Can’t I just …​ update?

8.1.1 A brief intro to changesets

8.1.2 Creating a changeset for Auction.Item

8.2 Now you can update!

8.2.1 Adjusting the public interface for updating


9 Transforming data in your browser

9.1 Handling new routes in your application

9.1.1 Adding a new route

9.1.2 Adding a new controller

9.1.3 Adding a new view

9.1.4 Adding a new template

9.2 Viewing the details of a single item

9.2.1 Defining the show route and function

9.2.2 Defining the show.html template

9.2.3 Linking to individual items

9.3 Creating items through web forms

9.3.1 Defining the new and create routes

9.3.2 Defining the “new” controller function

9.3.3 Defining the “new” template

9.3.4 Defining the create controller function

9.4 Editing items through web forms

9.4.1 Defining the edit and update routes

9.4.2 Defining the “edit” controller function

9.4.3 Defining the edit.html.eex template

9.4.4 Defining AuctionWeb.ItemController.update/2


10 Plugs, assigns, and dealing with session data

10.1 Preparing your application for user registration

10.1.1 Defining the Auction.User schema

10.1.2 Creating a migration to create the users table

10.1.3 Defining changesets for Auction.User

10.1.4 Creating API functions in Auction

10.1.5 User registration web flow

10.2 Handling user login and sessions

10.2.1 Creating the route, controller, view, and templates for logging in and out

10.2.2 Implementing the dirty details of sessions and authentication

10.3 Plugs

10.4 Adding site navigation

10.4.1 Implementing the log out function

10.5 Restricting users from certain pages


11 Associating records and accepting bids

11.1 Creating bids

11.2 Adding associations to the Auction.Bid schema

11.2.1 A little detour into associations

11.2.2 Creating a migration that describes the associations

11.2.3 Creating a form to accept bids

11.3 Using has_many with items and users

11.3.1 Preloading and avoiding N+1 queries

11.3.2 Preloading associations in the public interface module and controller

11.3.3 Adding the list of bids to the view template

11.4 Listing a user’s bids on their profile page

11.4.1 Using Ecto.Query to construct database queries

11.4.2 Making a view helper function global and using the bids assign

11.4.3 Using Timex to format dates and times

11.5 Some ideas for further improvement


Part 3: Those important extras

12 Using Phoenix channels for real-time communication

12.1 What are Phoenix channels?

12.2 Connecting a user to a channel and a topic

12.2.1 Handling topic join requests

12.2.2 Getting the user’s browser to join a topic

12.3 Sending real-time messages to a user

12.3.1 Receiving messages in the user’s browser

12.3.2 Configuring the channel to handle messages

12.3.3 Sending your browser a message from IEx

12.4 Updating all users when a new bid is made

12.4.1 Refactoring the rendering of an item’s bids

12.4.2 Modifying the controller to broadcast a message

12.4.3 Handling the broadcasted HTML in the browser


13 Building an API

13.1 Scoping API requests to a new controller

13.2 Creating the AuctionWeb.Api.ItemController controller and view


14 Testing in Elixir and Phoenix

14.1 An introduction to ExUnit

14.1.1 Writing a first test

14.2 Setting up tests for Ecto

14.2.1 Configuring a new database

14.2.2 Creating a sandbox and configuring Ecto to use it in tests

14.2.3 Setting up the database before tests run

14.2.4 Creating a Mix alias for use during test runs

14.3 Testing Ecto queries in Auction

14.3.1 Testing Auction.list_items/0

14.3.2 Testing Auction.get_item/1

14.3.3 Testing Auction.insert_item/1

14.4 Simultaneously writing documentation and tests with doctests

14.4.1 Writing function-level documentation

14.4.2 Viewing documentation in IEx

14.4.3 Viewing documentation with ExDoc

14.4.4 Adding examples and doctests

14.4.5 Module-level documentation

14.5 Writing tests For Phoenix

14.5.1 Testing that an item is added to the database on success

14.5.2 Testing that you’re redirected to the new item on success

14.5.3 Testing that no item is created on error

14.5.4 Testing that the new item form is rendered on error

14.5.5 The entire ItemControllerTest module

14.6 What next?



Appendix A: Installing Elixir and Phoenix

A.1 Installing Elixir

A.1.1 macOS and Unix/Linux variants

A.1.2 Windows

A.1.3 From source

A.1.4 Installing Erlang

A.2 Installing Phoenix

A.2.1 PostgreSQL

A.2.2 Node.js

Appendix B: More Elixir resources

On the web



What's inside

  • Functional programming in a web environment
  • An introduction to Elixir
  • Database interactions with Ecto
  • Real-time communication with channels

About the reader

For web developers familiar with a framework like Rails or ASP.NET. No experience with Elixir or Phoenix required.

About the author

Geoffrey Lessel is a seasoned web developer who speaks and blogs about Elixir and Phoenix.

placing your order...

Don't refresh or navigate away from the page.
print book $29.99 $49.99 pBook + eBook + liveBook
Additional shipping charges may apply
Phoenix in Action (print book) added to cart
continue shopping
go to cart

eBook $31.99 $39.99 3 formats + liveBook
Phoenix in Action (eBook) added to cart
continue shopping
go to cart

Prices displayed in rupees will be charged in USD when you check out.
customers also reading

This book 1-hop 2-hops 3-hops

FREE domestic shipping on three or more pBooks