O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Phoenix Web Development

Book Description

Learn to build a high-performance functional prototype of a voting web application from scratch using Elixir and Phoenix

About This Book
  • Build a strong foundation in Functional-Programming techniques while learning to build compelling web applications
  • Understand the Elixir Concurrency and parallelization model to build high-performing blazingly fast applications
  • Learn to test, debug and deploy your web applications using Phoenix framework
Who This Book Is For

This book is for people with a basic knowledge of Elixir, who want to start building web applications. Prior experience with web technologies is assumed.

What You Will Learn
  • Learn Phoenix Framework fundamentals and v1.3's new application structure
  • Build real-time applications with channels and presence
  • Utilize GenServers and other OTP fundamentals to keep an application stable
  • Track users as they sign in and out of chat with Phoenix's built-in presence functionality
  • Write your own database interaction code that is safe, bug-free, and easy to work with
  • Explore testing and debugging methodologies to understand a real software development lifecycle for a Phoenix application
  • Deploy and run your Phoenix application in production
In Detail

Phoenix is a modern web development framework that is used to build API's and web applications. It is built on Elixir and runs on Erlang VM which makes it much faster than other options. With Elixir and Phoenix, you build your application the right way, ready to scale and ready for the increasing demands of real-time web applications.

This book covers the basics of the Phoenix web framework, showing you how to build a community voting application, and is divided into three parts. In the first part, you will be introduced to Phoenix and Elixir and understand the core terminologies that are used to describe them. You will also learn to build controller pages, store and retrieve data, add users to your app pages and protect your database. In the second section you will be able to reinforce your knowledge of architecting real time applications in phoenix and not only debug these applications but also diagnose issues in them. In the third and final section you will have the complete understanding of deploying and running the phoenix application and should be comfortable to make your first application release

By the end of this book, you'll have a strong grasp of all of the core fundamentals of the Phoenix framework, and will have built a full production-ready web application from scratch.

Style and approach

Covers the basics of the Phoenix web framework by building a complete real-time application and showing the new structure changes introduced in Phoenix v1.3

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Phoenix Web Development
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  6. A Brief Introduction to Elixir and Phoenix
    1. Introducing IEx and Elixir
      1. What is IEx?
      2. Variables in Elixir
      3. Immutability in Elixir
      4. Understanding the different types in Elixir
        1. Getting more information with the i helper
        2. Getting more information with the h helper
        3. Using IEx and helpers to understand types
      5. Your objects have no power here
    2. Introduction to Phoenix
      1. Installing Phoenix 1.3
    3. Creating a new Phoenix project
      1. Running the Phoenix Mix Task
      2. Running the Phoenix server for the first time
      3. Phoenix's default application structure
        1. Configuration files
        2. Assets files
        3. Private files
        4. Tests
        5. Other directories
        6. The most important directory: lib
      4. A note about how data flows in Phoenix
    4. Summary
  7. Building Controllers, Views, and Templates
    1. Understanding the flow of Phoenix connections
    2. Creating our Social Voting project
    3. Creating a poll controller
      1. Understanding the controller's structure
      2. Building the poll controller
      3. Understanding templates
      4. Passing data to our templates
    4. Writing controller tests
      1. Understanding the code behind tests
      2. Writing the poll controller test
    5. Summary
  8. Storing and Retrieving Vote Data with Ecto Pages
    1. Understanding the role of schemas
    2. Creating a new migration
      1. Creating the Polls table migration
      2. Creating our Options table migration
    3. Creating our Poll schema
      1. Testing our Poll schema
    4. Creating our Option schema
      1. Understanding the gotchas of associations
    5. Understanding the role of contexts
      1. Creating a Votes context
      2. Grabbing a list of data
      3. Understanding Ecto query writing
      4. Hooking up the context to our controller
    6. Creating a new poll
      1. Creating the new action in the controller
      2. Creating our create function
    7. Writing our unit tests
    8. Summary
  9. Introducing User Accounts and Sessions
    1. Adding user accounts
      1. Designing our user schema
      2. Creating our user schema
      3. Creating our accounts context
        1. Writing our user unit tests
      4. Creating a user signup page
        1. Creating the routes
        2. Creating the controller code (with tests)
      5. Setting up the password functionality
        1. Installing Comeonin
        2. Adding Comeonin to the user schema file
        3. Updating our tests
        4. Updating the UI to include password fields
      6. Creating a user login page
        1. Building our create session function
        2. Writing session controller tests
    2. Summary
  10. Validations, Errors, and Tying Loose Ends
    1. Connecting polls to users
      1. Creating the migration
      2. Modifying the schemas
      3. Fixing broken poll tests
      4. Sending a user ID through the controller
        1. Retrieving data from sessions
        2. Writing our Poll Controller's tests
      5. Restricting access via sessions
      6. Working with validations and errors
        1. Making usernames unique
        2. Writing custom validations
        3. Displaying validation errors in our forms
    2. Summary
  11. Live Voting with Phoenix
    1. Building channels and topics in Phoenix
      1. Understanding sockets
      2. Understanding channels
      3. Working with ES2015 syntax
        1. Imports and exports
        2. let and const
        3. Fat-arrow functions
        4. Variable and argument destructuring
      4. Sending and receiving messages with channels
        1. Conditionally loading our socket
        2. Sending messages on the socket
      5. Allowing users to vote on polls
      6. Making voting real-time
        1. Building our dummy functionality
        2. Changing our dummy code to push to the server
        3. Writing our server channel code for live voting
        4. Refactoring our channels away from the index
        5. Moving the channel functionality to show
      7. Starting our channel tests
    2. Summary
  12. Improving Our Application and Adding Features
    1. Designing and implementing our new features
      1. Implementing file uploads in Phoenix
        1. Working with uploads in Phoenix
        2. Adding file uploads to our new poll UI
      2. Hooking up the uploads to our database
        1. Writing the migration file
        2. Modifying the schema and the context code
        3. Completing the votes context for the image uploads
      3. Implementing voting restrictions
        1. Creating the vote record migration
        2. Creating the vote record schema
        3. Hooking up restrictions
      4. Fixing the broken tests
    2. Summary
  13. Adding Chat to Your Phoenix Application
    1. Adding chat to a Phoenix application
      1. Working with the chat schema
        1. Building the chat schema
        2. Designing our message functionality
        3. Implementing message functions in our context
        4. Writing our unit tests
      2. Fixing navigation in our application
      3. Creating the chat UI
        1. Building the UI Itself
      4. Creating our chat channel
        1. Sending chat messages
        2. Hooking up the new JavaScript code to Phoenix
        3. Refactoring for poll chats
      5. Fixing up our tests
        1. Returning to a passing test suite
    2. Summary
  14. Using Presence and ETS in Phoenix
    1. Utilizing Presence and ETS to make our app more robust
      1. What is Presence?
        1. Updating our chat UI
        2. Elixir implementation
        3. JavaScript implementation
      2. Using ETS
        1. Why use ETS?
        2. Experimenting with ETS in an IEx window
      3. Creating our Presence ETS table and GenServer
        1. Setting up the GenServer
        2. Creating the public interface for the GenServer
        3. Implementing the cast and call logic
        4. Hooking up the GenServer to our application
      4. Storing Presence data in ETS
        1. Retrieving Presence data in ETS
    2. Summary
  15. Working with Elixir's Concurrency Model
    1. Introduction to Elixir's concurrency model
      1. The difference between concurrency and parallelism
        1. In process 1
        2. Run process 1
          1. In process 1
          2. In process 2
        3. Run process 1 and process 2 at the same time
      2. Talking about OTP/understanding the model
        1. Working with an example
      3. Diving deeper into the concurrency model
        1. The model - what is a process?
        2. The model - what if our process crashes?
        3. The model - what is a task?
        4. The model - what is an agent?
        5. The model - what is a supervisor?
        6. The model - what is an application?
      4. Using GenServers
    2. Summary
  16. Implementing OAuth in Our Application
    1. Solidifying the new user experience
      1. Shoring up our tests
      2. Building a good development seeds file
      3. Hooking up our polls index
    2. Adding Ueberauth support
      1. Adding OAuth login support for Twitter with Ueberauth
        1. Setting up our application with Twitter
        2. Configuring the Twitter login process in Phoenix
        3. Modifying the users schema
        4. Implementing the Twitter login in Phoenix
      2. Adding OAuth login support for Google with Ueberauth
        1. Configuring Google to allow OAuth
        2. Configuring Ueberauth in Google
        3. Implementing Google OAuth for Ueberauth and Phoenix
    3. Summary
  17. Building an API and Deploying
    1. Building our API
      1. Building an API in Code
        1. Expanding Our API Request
      2. Authenticating Against our API
        1. Allowing a user to navigate to their profile page
        2. Introducing API keys to the database
        3. Validating API Keys
      3. Dealing with Error Handling in APIs
        1. Implementing an API Resource Show
        2. Adding an Error Handler for 404s for JSON
    2. Deploying Phoenix applications to production
      1. Initial requirements for deployment into production
        1. Alternative Deployment Strategies
    3. Summary
  18. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think