Early Praise for Programming Phoenix 1.4 Programming Phoenix offers a very engaging hands-on approach without compro- mising depth in content, making it a balanced source of knowledge for beginners and hackers alike. The authors’ credibility comes not only from the fact that they are creators of Elixir and Phoenix, but for their experience in the field designing, building, and scaling big apps—and that completely shows in this book. ➤ João Augusto B.C. Alves Software Consultant, Plataformatec Programming Phoenix will provide you with the most in-depth, cutting-edge insights into how to harness the full power of the Phoenix framework. If you want to be the best, learn from the best. ➤ Tetiana Dushenkivska Creator of ElixirCards, Clever Bunny I write Elixir for a living, and Programming Phoenix was exactly what I needed. It filled in the sticky details, like how to tie authentication into web applications and channels. It also showed me how to layer services with OTP. The experience of Chris and José makes all of the difference in the world. ➤ Eric Meadows-Jönsson Elixir Core Team Phoenix gives you all the tools needed to handle very complex problems in a very elegant way. Programming Phoenix gives you all the tips you need to solve such problems. It’s a must have. ➤ Marcos Ramos Senior Elixir Developer, Plataformatec Even if you have no current plans to write a Phoenix web app, you need to read Programming Phoenix. The insights this book gives into Elixir, Erlang, and OTP— their strengths, and the corresponding thoughtful design patterns that went into the Phoenix framework—are invaluable to any developer in the Elixir/Erlang ecosystem. ➤ Mike Binns Senior Software Engineer, Dockyard Programming Phoenix ≥ 1.4 Productive |> Reliable |> Fast Chris McCord Bruce Tate José Valim The Pragmatic Bookshelf Raleigh, North Carolina Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade- marks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic books, screencasts, and audio books can help you and your team create better software and have more fun. Visit us at https://pragprog.com. The team that produced this book includes: Publisher: Andy Hunt VP of Operations: Janet Furlow Managing Editor: Susan Conant Development Editor: Jacquelyn Carter Copy Editor: Jasmine Kwityn Indexing: Potomac Indexing, LLC Layout: Gilson Graphics For sales, volume licensing, and support, please contact [email protected]. For international rights, please contact [email protected]. Copyright © 2019 The Pragmatic Programmers, LLC. 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: 978-1-68050-226-8 Encoded using the finest acid-free high-entropy binary digits. Book version: P1.0—October 2019 Contents Acknowledgments . . . . . . . . . . . ix Preface . . . . . . . . . . . . . . xiii 1. Introducing Phoenix . . . . . . . . . . . 1 Productive 2 Concurrent 4 Beautiful Code 8 Interactive 10 Reliable 13 Part I — Building with Functional MVC 2. The Lay of the Land . . . . . . . . . . . 17 Simple Functions 17 Installing Your Development Environment 20 Creating a Throwaway Project 22 Building a Feature 23 Going Deeper: The Request Pipeline 30 Wrapping Up 38 3. Controllers . . . . . . . . . . . . . 41 Understanding Controllers 41 Building a Controller 49 Coding Views 50 Using Helpers 52 Showing a User 54 Wrapping Up 58 4. Ecto and Changesets . . . . . . . . . . 59 Understanding Ecto 59 Contents • vi Defining the User Schema and Migration 60 Using the Repository to Add Data 63 Building Forms 66 Creating Resources 70 Wrapping Up 75 5. Authenticating Users . . . . . . . . . . 77 Preparing for Authentication 77 Managing Registration Changesets 79 Creating Users 82 The Anatomy of a Plug 85 Writing an Authentication Plug 88 Implementing Login and Logout 93 Presenting User Account Links 97 Wrapping Up 100 6. Generators and Relationships . . . . . . . . 101 Using Generators 101 Building Relationships 110 Managing Related Data 113 In-context Relationships 117 Wrapping Up 122 7. Ecto Queries and Constraints . . . . . . . . 123 Seeding and Associating Categories 123 Diving Deeper into Ecto Queries 130 Constraints 136 Wrapping Up 143 8. Testing MVC . . . . . . . . . . . . 145 Understanding ExUnit 146 Testing Contexts 150 Using Ecto Sandbox for Test Isolation and Concurrency 157 Integration Tests 158 Unit-Testing Plugs 167 Testing Views and Templates 172 Wrapping Up 174 Contents • vii Part II — Writing Interactive and Maintainable Applications 9. Watching Videos . . . . . . . . . . . 177 Watching Videos 177 Adding JavaScript 181 Creating Slugs 186 Wrapping Up 192 10. Using Channels . . . . . . . . . . . 193 The Channel 194 Phoenix Clients with ES6 195 Preparing Our Server for the Channel 198 Creating the Channel 200 Sending and Receiving Events 202 Socket Authentication 207 Persisting Annotations 210 Handling Disconnects 217 Tracking Presence on a Channel 220 Wrapping Up 226 11. Observer and Umbrellas . . . . . . . . . 229 Introspecting Applications with Observer 230 Using Umbrellas 233 Extracting Rumbl and RumblWeb 236 Wrapping Up 241 12. OTP . . . . . . . . . . . . . . 243 Managing State with Processes 243 Building GenServers for OTP 247 Designing an Information System with OTP 257 Building the Wolfram Info System 265 Integrating OTP Services with Channels 274 Wrapping Up 277 13. Testing Channels and OTP . . . . . . . . . 279 Testing the Information System 280 Isolating Wolfram 285 Adding Tests to Channels 289 Authenticating a Test Socket 290 Communicating with a Test Channel 291 Wrapping Up 296 Contents • viii 14. What’s Next? . . . . . . . . . . . . 297 Other Interesting Features 298 Phoenix LiveView 302 Phoenix PubSub 2.0 313 Phoenix and Telemetry Integration 314 Good Luck! 316 Index . . . . . . . . . . . . . . 317