Real-Time Phoenix
Build Highly Scalable Systems with Channels
by Stephen Bussey
Version: B3.0 (October 18, 2019)
Copyright 2019 The Pragmatic Programmers, LLC. This book is licensed to the individual who purchased it. We don't copy-protect it because that would limit your ability to use it for your own purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copy with other members of your team, with friends, or via file sharing services. Thanks.
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 and the linking g device are trademarks 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.
About the Pragmatic Bookshelf
The Pragmatic Bookshelf is an agile publishing company. Were here because we want to improve the lives of developers. We do this by creating timely, practical titles, written by programmers for programmers.
Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com.
Our ebooks do not contain any Digital Restrictions Management, and have always been DRM-free. We pioneered the beta book concept, where you can purchase and read a book while its still being written, and provide feedback to the author to help make a better book for everyone. Free resources for all purchasers include source code downloads (if applicable), errata and discussion forums, all available on the book's home page at pragprog.com. Were here to make your life easier.
New Book Announcements
Want to keep up on our latest titles and announcements, and occasional special offers? Just create an account on pragprog.com (an email address and a password is all it takes) and select the checkbox to receive newsletters. You can also follow us on twitter as @pragprog.
About Ebook Formats
If you buy directly from pragprog.com, you get ebooks in all available formats for one price. You can synch your ebooks amongst all your devices (including iPhone/iPad, Android, laptops, etc.) via Dropbox. You get free updates for the life of the edition. And, of course, you can always come back and re-download your books when needed. Ebooks bought from the Amazon Kindle store are subject to Amazon's polices. Limitations in Amazon's file format may cause ebooks to display differently on different devices. For more information, please see our FAQ at pragprog.com/frequently-asked-questions/ebooks. To learn more about this book and access the free resources, go to https://pragprog.com/book/sbsockets, the book's homepage.
Thanks for your continued support,
Andy Hunt
The Pragmatic Programmers
Table of Contents
Copyright 2019, The Pragmatic Bookshelf.
Change History
The book youre reading is in beta. This means that we update itfrequently. Here is the list of the major changes that have been madeat each beta release of the book, with the most recent change first.
B3.0: October 18, 2019
We added a new chapter: Chapter 9,
Addressed errata. Thanks for all of your help and keep them coming!
B2.0: September 19, 2019
We added a new chapter: Chapter 8,
Addressed errata. Thanks for all of your help and keep them coming!
B1.0: August 28, 2019
Copyright 2019, The Pragmatic Bookshelf.
Introduction
I remember working on the first production-facing Elixir application I wroteit was a real-time application todrive an innovative new feature for our platform. Excitement, curiosity, and a goodbit of nervousness led me through that project. It was a trial by fire as the applicationwould receive more requests per minute than any other part of our platform. It still stands todaywithout much involvement needed over the past years.
I have gotten to work on many other real-time applications since that first project. Thelessons I learned were sometimes hard to come bya critical piece of the application wouldfail, applications would overuse resources like CPU and RAM, or I would code something in a non-optimal way.However, I was able to leverage the strong foundations of Elixir and Phoenix to solve any problem that appeared. Thisbook aims to collect the experience that I have gained working with Elixir and Phoenix Channelsover the last several years and distill it into the parts that matter most.
Elixir has changed the way that I think about, design, and code applications. The creators,community, and libraries empower me to think about code with a fresh perspective. My time withElixir has been filled with enthusiasm, to say the least. Throughout this book, I hope to share thatenthusiasm with you.
Who Should Read This Book?
Do you work on modern web applications? Do you want to build applications that are different than the traditional web model ofrequest-in response-out? Have you started working on Elixir or Phoenix projects and want to dive deeper into the ecosystem?
If any of these questions ring a bell for you, then you will probably have a good experience with this book. If not, youwill still find an interesting approach to modern applications in these pages.
This book is targeted at intermediate to advanced developers. There will be Elixir code snippets throughouteach chapter, but you will be guided through each of them in order to have working examples locally.This book will not teach you Elixirthere are other books out there that are suited for that task. However,you will quickly catch on if you have a small amount of existing Elixir or Phoenix knowledge. You will walkaway with a deep understanding of the real-time Phoenix stack.
About This Book
The three parts of this book build on each other to teach you about WebSockets, Phoenix Channels,and real-time application design. Part I focuses on the most important part of the real-time stack inElixirPhoenix Channels, WebSockets, and GenStage. Well cover a lot of ground in these chapters and youwill gain the foundations necessary to build real-time applications.
You will leverage the foundations from Part I when we work on a real-time application in Part II. You willadd real-time features to an e-commerce application that serves many users simultaneously. You will also getto work with some of the more advanced features of Phoenix, such as Phoenix Tracker.
Part III finishes the book with guidance on running real-time applications in production. The battle is onlybeginning when you write an application. You have to then keep it healthy and happy in production.
About the Code
Elixir is required for this book, although setup is a bit outside of this books focus. I recommend usinga version manager like asdf in order to get Phoenix setup.