91
ELIXIR/PHOENIX BUILD YOUR OWN REAL-TIME WEB SERVICE WITH

Build Your Own Real-Time Web Service with Elixir Phoenix

Embed Size (px)

Citation preview

Page 1: Build Your Own Real-Time Web Service with Elixir Phoenix

ELIXIR/PHOENIXBUILD YOUR OWN REAL-TIME WEB SERVICE WITH

Page 2: Build Your Own Real-Time Web Service with Elixir Phoenix

PRESENTED BY: CHI-CHI EKWEOZOR AT:

MANCHESTER LAMBDA LOUNGE MON 17 OCT 2016

Page 3: Build Your Own Real-Time Web Service with Elixir Phoenix

TODAY’S WEB:

Hundreds of millions of interconnected services continuously exchanging data.

Reliability, concurrency, fault-tolerance, scalability, all expected as the norm.

Page 4: Build Your Own Real-Time Web Service with Elixir Phoenix

WHY ELIXIR? WHY PHOENIX?

BUILDING REAL-TIME WEB SERVICES

Page 5: Build Your Own Real-Time Web Service with Elixir Phoenix

ABOUT ME:

▸ Chi-chi Ekweozor (@thisischichi):

▸ Contract Software engineer - C/C++, PHP, JavaScript, and now Elixir

▸ Previously a social media marketing consultant!

▸ Combined social media know how with Elixir/Phoenix to create Assenty, a real-time Question & Answer tool for event organisers.

Page 6: Build Your Own Real-Time Web Service with Elixir Phoenix

INTENDED AUDIENCE:

This talk is intended for web developers with some familiarity with functional programming concepts.

It is a high level overview of the Phoenix web framework which is written in Elixir, a relatively new functional programming language.

No prior experience with Elixir is required.

Page 7: Build Your Own Real-Time Web Service with Elixir Phoenix

WE’LL COVER:

▸ Why Elixir?

▸ What is Phoenix?

▸ What are Phoenix Channels?

▸ How do they work?

▸ Assenty Live Demo: Phoenix Channels in Production

Page 8: Build Your Own Real-Time Web Service with Elixir Phoenix

IS IT FUNCTIONAL?WHY ELIXIR?

Page 9: Build Your Own Real-Time Web Service with Elixir Phoenix

ELIXIR IS A DYNAMIC, FUNCTIONAL LANGUAGE DESIGNED FOR BUILDING …

elixir-lang.org

Page 10: Build Your Own Real-Time Web Service with Elixir Phoenix

SCALABLE AND MAINTAINABLE APPLICATIONS.

elixir-lang.org

Page 11: Build Your Own Real-Time Web Service with Elixir Phoenix

ELIXIR LEVERAGES THE ERLANG VM, KNOWN FOR RUNNING LOW-LATENCY,

elixir-lang.org

Page 12: Build Your Own Real-Time Web Service with Elixir Phoenix

DISTRIBUTED AND FAULT-TOLERANT SYSTEMS, WHILE ALSO BEING SUCCESSFULLY

elixir-lang.org

Page 13: Build Your Own Real-Time Web Service with Elixir Phoenix

USED IN WEB DEVELOPMENT AND THE EMBEDDED SOFTWARE DOMAIN.

elixir-lang.org

Page 14: Build Your Own Real-Time Web Service with Elixir Phoenix
Page 15: Build Your Own Real-Time Web Service with Elixir Phoenix

ELIXIR’S STRENGTHS:

Page 16: Build Your Own Real-Time Web Service with Elixir Phoenix

WHY ELIXIR?

▸ An extensible and well-documented dynamic language with Ruby-inspired syntax, undergirded by Erlang’s 30 year old, tried and tested virtual machine known as BEAM

▸ In-built pattern matching and the pipe operator |> makes it easy to work with and reason about immutable data

▸ Mix: an impressive tooling ecosystem that adds rock solid libraries for authentication, metrics, crypto, embedded systems support and much more

Page 17: Build Your Own Real-Time Web Service with Elixir Phoenix

RATIONALE FOR CREATING ELIXIR:

Page 18: Build Your Own Real-Time Web Service with Elixir Phoenix

“WHEN DESIGNING THE ERLANG LANGUAGE AND THE ERLANG VM,

José Valim, creator, Elixir

Page 19: Build Your Own Real-Time Web Service with Elixir Phoenix

JOE, MIKE AND ROBERT DID NOT AIM TO IMPLEMENT A FUNCTIONAL PROGRAMMING LANGUAGE,

José Valim, creator, Elixir

Page 20: Build Your Own Real-Time Web Service with Elixir Phoenix

THEY WANTED A RUNTIME WHERE THEY COULD BUILD DISTRIBUTED, FAULT-TOLERANT APPLICATIONS.

José Valim, creator, Elixir

Page 21: Build Your Own Real-Time Web Service with Elixir Phoenix

IT JUST HAPPENED THAT THE FOUNDATION FOR WRITING SUCH SYSTEMS SHARE MANY OF THE FUNCTIONAL

José Valim, creator, Elixir

Page 22: Build Your Own Real-Time Web Service with Elixir Phoenix

PROGRAMMING PRINCIPLES. AND IT REFLECTS IN BOTH ERLANG AND ELIXIR.

José Valim, creator, Elixir

Page 23: Build Your Own Real-Time Web Service with Elixir Phoenix

BENEFIT OF CODING IN ELIXIR:

Page 24: Build Your Own Real-Time Web Service with Elixir Phoenix

AT LEAST TWO THINGS YOU STAND TO GAIN WITH ELIXIR:

▸ Because of data immutability, concurrency is a lot easier to understand, and use:

▸ Your application or program will use all available cores on your machine to function. It just does.

▸ A single function definition lets you define different implementations depending on its arguments:

▸ Your application or program will be easier to modularise and test.

Page 25: Build Your Own Real-Time Web Service with Elixir Phoenix

WE’LL COVER:

▸ Why Elixir?

▸ What is Phoenix?

▸ What are Phoenix Channels?

▸ How do they work?

▸ Assenty Live Demo: Phoenix Channels in Production

Page 26: Build Your Own Real-Time Web Service with Elixir Phoenix

IS IT REAL-TIME?AND THIS PHOENIX?

Page 27: Build Your Own Real-Time Web Service with Elixir Phoenix

INTRODUCING PHOENIX:

▸ Phoenix is a web development framework written in Elixir which implements the server-side MVC pattern.

▸ Many of its components and concepts will seem familiar to those of us with experience in other web frameworks like Ruby on Rails or Python's Django.

▸ Phoenix is made up of a number of distinct parts, each with its own purpose and role to play in building a web application.

Excerpted from the Phoenix Framework Guides

Page 28: Build Your Own Real-Time Web Service with Elixir Phoenix

ENDPOINTPHOENIX FRAMEWORK COMPONENT:

Page 29: Build Your Own Real-Time Web Service with Elixir Phoenix

HANDLES ALL ASPECTS OF REQUESTS UP UNTIL THE POINT WHERE THE ROUTER TAKES OVER

Page 30: Build Your Own Real-Time Web Service with Elixir Phoenix

ROUTERPHOENIX FRAMEWORK COMPONENT:

Page 31: Build Your Own Real-Time Web Service with Elixir Phoenix

PARSES INCOMING REQUESTS AND DISPATCHES THEM TO THE CORRECT CONTROLLER/ACTION, PASSING PARAMETERS AS NEEDED

Page 32: Build Your Own Real-Time Web Service with Elixir Phoenix

MODELSPHOENIX FRAMEWORK COMPONENT:

Page 33: Build Your Own Real-Time Web Service with Elixir Phoenix

USES ECTO, AN ELIXIR FRAMEWORK FOR PERSISTENCE TO READ AND PERSIST DATA TO AN UNDERLYING DATABASE

Page 34: Build Your Own Real-Time Web Service with Elixir Phoenix

CONTROLLERSPHOENIX FRAMEWORK COMPONENT:

Page 35: Build Your Own Real-Time Web Service with Elixir Phoenix

PROVIDE FUNCTIONS, CALLED ACTIONS, TO HANDLE REQUESTS

Page 36: Build Your Own Real-Time Web Service with Elixir Phoenix

VIEWSPHOENIX FRAMEWORK COMPONENT:

Page 37: Build Your Own Real-Time Web Service with Elixir Phoenix

RENDER TEMPLATES AND ACT AS A PRESENTATION LAYER

Page 38: Build Your Own Real-Time Web Service with Elixir Phoenix

TEMPLATESPHOENIX FRAMEWORK COMPONENT:

Page 39: Build Your Own Real-Time Web Service with Elixir Phoenix

HTML FILES INTO WHICH WE PASS DATA TO FORM COMPLETE HTTP RESPONSES

Page 40: Build Your Own Real-Time Web Service with Elixir Phoenix

CHANNELSPHOENIX FRAMEWORK COMPONENT:

Page 41: Build Your Own Real-Time Web Service with Elixir Phoenix

MANAGE (WEB) SOCKETS FOR EASY REAL-TIME COMMUNICATION

Page 42: Build Your Own Real-Time Web Service with Elixir Phoenix

PUBSUBPHOENIX FRAMEWORK COMPONENT:

Page 43: Build Your Own Real-Time Web Service with Elixir Phoenix

UNDERGIRDS THE CHANNEL LAYER AND ALLOWS CLIENTS TO SUBSCRIBE TO TOPICS

Page 44: Build Your Own Real-Time Web Service with Elixir Phoenix

WE’LL COVER:

▸ Why Elixir?

▸ What is Phoenix?

▸ What are Phoenix Channels?

▸ How do they work?

▸ Assenty Live Demo: Phoenix Channels in Production

Page 45: Build Your Own Real-Time Web Service with Elixir Phoenix
Page 46: Build Your Own Real-Time Web Service with Elixir Phoenix

WHAT ARE PHOENIX CHANNELS?

▸ Channels are a really exciting and powerful part of Phoenix that allow us to easily add soft real-time features to our applications.

▸ Channels are based on a simple idea - sending and receiving messages. Senders broadcast messages about topics.

▸ Receivers subscribe to topics so that they can get those messages. Senders and receivers can switch roles on the same topic at any time.

Excerpted from the Phoenix Framework Guides

Page 47: Build Your Own Real-Time Web Service with Elixir Phoenix

CHARACTERISTICS OF SOFT REAL-TIME SYSTEMS:

▸ You don’t have to hit every deadline, as opposed to the case in hard real-time systems.

▸ Performance will degrade if too many are missed but results are still valid after the deadline.

▸ Examples: airline reservation systems.

▸ Contrast with hard real-time systems where you must absolutely hit every deadline. Examples: nuclear systems, aircraft control systems, defence applications.

Page 48: Build Your Own Real-Time Web Service with Elixir Phoenix

TRADITIONAL STATELESS REQUEST-RESPONSE MODEL:Image taken from Programming Phoenix by Chris McCord, Bruce Tate, and José Valim

Page 49: Build Your Own Real-Time Web Service with Elixir Phoenix

PHOENIX CHANNELS GIVE YOU STATEFUL CONVERSATIONS:Image taken from Programming Phoenix by Chris McCord, Bruce Tate, and José Valim

Page 50: Build Your Own Real-Time Web Service with Elixir Phoenix

A SINGLE CLIENT ON A PAGE CONNECTS DIRECTLY WITH A PROCESS ON THE SERVER…

Chris McCord, Phoenix creator

Page 51: Build Your Own Real-Time Web Service with Elixir Phoenix

CALLED A CHANNEL.

Chris McCord, Phoenix creator

Page 52: Build Your Own Real-Time Web Service with Elixir Phoenix

SINCE ELIXIR CAN SCALE TO MILLIONS OF SIMULTANEOUS PROCESSES THAT MANAGE

Chris McCord, Phoenix creator

Page 53: Build Your Own Real-Time Web Service with Elixir Phoenix

MILLIONS OF CONCURRENT CONNECTIONS, YOU DO NOT HAVE TO RESORT TO THE …

Chris McCord, Phoenix creator

Page 54: Build Your Own Real-Time Web Service with Elixir Phoenix

REQUEST/RESPONSE MODEL TO MAKE THINGS EASY TO SCALE OR EVEN MANAGE.

Chris McCord, Phoenix creator

Page 55: Build Your Own Real-Time Web Service with Elixir Phoenix

A CLIENT CONNECTS TO A CHANNEL AND THEN SENDS AND RECEIVES MESSAGES.

Chris McCord, Phoenix creator

Page 56: Build Your Own Real-Time Web Service with Elixir Phoenix

THAT’S IT.

Chris McCord, Phoenix creator

Page 57: Build Your Own Real-Time Web Service with Elixir Phoenix

WHAT PHOENIX CHANNELS MAKE POSSIBLE:

▸ With Channels, neither senders nor receivers have to be Elixir processes.

▸ They can be anything that we can teach to communicate over a Channel - a JavaScript client, an iOS app, another Phoenix application, our watch.

▸ Whereas request/response interactions are stateless, conversations in a long-running process can be stateful.

Excerpted from the Phoenix Framework Guides

Page 58: Build Your Own Real-Time Web Service with Elixir Phoenix

WE’LL COVER:

▸ Why Elixir?

▸ What is Phoenix?

▸ What are Phoenix channels?

▸ How do they work?

▸ Assenty Live Demo: Phoenix Channels in Production

Page 59: Build Your Own Real-Time Web Service with Elixir Phoenix

WHAT’S INSIDE?UNDERSTANDING PHOENIX CHANNELS

Page 60: Build Your Own Real-Time Web Service with Elixir Phoenix
Page 61: Build Your Own Real-Time Web Service with Elixir Phoenix

HOW DO PHOENIX CHANNELS WORK?

▸ A Phoenix channel is a conversation.

▸ The channel sends messages, receives messages, and keeps state. We call the messages events.

▸ A Phoenix conversation is about a topic, and it maps onto application concepts like a chat room, a game, or in Assenty's case, the question board for an event.

Excerpted from Programming Phoenix by Chris McCord, Bruce Tate, and José Valim

Page 62: Build Your Own Real-Time Web Service with Elixir Phoenix

BENEFIT OF USING PHOENIX CHANNELS:

▸ For sophisticated user interactions like interactive pages or multiplayer games, you don’t have to work so hard to keep track of the conversation by using cookies, databases, or the like.

▸ Each call to a channel simply picks up where the last one left off.

▸ A client establishes a new connection with a web socket. That socket is transformed through the life of the conversation with the server.

Excerpted from Programming Phoenix by Chris McCord, Bruce Tate, and José Valim

Page 63: Build Your Own Real-Time Web Service with Elixir Phoenix

COMPONENTS OF A PHOENIX CHANNEL:

Page 64: Build Your Own Real-Time Web Service with Elixir Phoenix
Page 65: Build Your Own Real-Time Web Service with Elixir Phoenix

USER SOCKETPHOENIX CHANNEL COMPONENT:

Page 66: Build Your Own Real-Time Web Service with Elixir Phoenix

THIS MODULE SERVES AS THE STARTING POINT FOR ALL SOCKET CONNECTIONS

Page 67: Build Your Own Real-Time Web Service with Elixir Phoenix

RESPONSIBLE FOR AUTHENTICATING, IT ALSO DEFINES THE TRANSPORT

Page 68: Build Your Own Real-Time Web Service with Elixir Phoenix

LAYERS THAT WILL HANDLE THE CONNECTION BETWEEN THE CLIENT AND THE SERVER.

Page 69: Build Your Own Real-Time Web Service with Elixir Phoenix

TOPICPHOENIX CHANNEL COMPONENT:

Page 70: Build Your Own Real-Time Web Service with Elixir Phoenix

JUST AS A CONTROLLER PASSES AN ID PARAMETER TO REPRESENT A RESOURCE

Page 71: Build Your Own Real-Time Web Service with Elixir Phoenix

FOR A CONTROLLER, WE USE A TOPIC ID TO SCOPE A CHANNEL CONNECTION.

Page 72: Build Your Own Real-Time Web Service with Elixir Phoenix

WHEN CLIENTS JOIN A CHANNEL, THEY MUST PROVIDE A TOPIC. CLIENTS CAN JOIN

Page 73: Build Your Own Real-Time Web Service with Elixir Phoenix

ANY NUMBER OF CHANNELS AND ANY NUMBER OF TOPICS ON A CHANNEL.

Page 74: Build Your Own Real-Time Web Service with Elixir Phoenix

CHANNELPHOENIX CHANNEL COMPONENT:

Page 75: Build Your Own Real-Time Web Service with Elixir Phoenix

THIS MODULE ALLOWS CONNECTIONS AND LETS USERS DISCONNECT AND SEND EVENTS

Page 76: Build Your Own Real-Time Web Service with Elixir Phoenix

IT SUPPORTS 4 CALLBACKS: JOIN(), HANDLE_IN(), HANDLE_OUT(), HANDLE_INFO()

Page 77: Build Your Own Real-Time Web Service with Elixir Phoenix

1. JOIN(): CLIENTS JOIN TOPICS ON A CHANNEL

Page 78: Build Your Own Real-Time Web Service with Elixir Phoenix

2. HANDLE_IN(): RECEIVES DIRECT CHANNEL EVENTS

Page 79: Build Your Own Real-Time Web Service with Elixir Phoenix

3. HANDLE_OUT(): INTERCEPTS BROADCAST EVENTS

Page 80: Build Your Own Real-Time Web Service with Elixir Phoenix

4. HANDLE_INFO(): RECEIVES PLATFORM (OTP) MESSAGES

Page 81: Build Your Own Real-Time Web Service with Elixir Phoenix

CLIENT LIBRARYPHOENIX CHANNEL COMPONENT:

Page 82: Build Your Own Real-Time Web Service with Elixir Phoenix

A CONSTRUCT THAT CAN CONNECT DIRECTLY TO THE SERVER

Page 83: Build Your Own Real-Time Web Service with Elixir Phoenix

PHOENIX CURRENTLY SHIPS WITH ITS OWN JAVASCRIPT CLIENT.

Page 84: Build Your Own Real-Time Web Service with Elixir Phoenix

IOS, ANDROID, AND C# CLIENTS HAVE BEEN RELEASED WITH PHOENIX 1.0.

Page 85: Build Your Own Real-Time Web Service with Elixir Phoenix

YOUR CLIENT CODEPHOENIX CHANNEL COMPONENT:

Page 86: Build Your Own Real-Time Web Service with Elixir Phoenix

THIS JAVASCRIPT OBJECT ADDS REAL-TIME FUNCTIONALITY TO YOUR APPLICATION BY LISTENING FOR EVENTS GENERATED BY THE CALLBACKS (JOIN(), HANDLE_* ETC) DEFINED IN ELIXIR AND RESPONDING APPROPRIATELY.

Page 87: Build Your Own Real-Time Web Service with Elixir Phoenix

WE’LL COVER:

▸ Why Elixir?

▸ What is Phoenix?

▸ What are Phoenix channels?

▸ How do they work?

▸ Assenty Live Demo: Phoenix Channels in Production

Page 88: Build Your Own Real-Time Web Service with Elixir Phoenix

WHAT IS ASSENTY?

▸ Assenty is a real-time question and answer tool for event organisers.

▸ Written in Elixir, and running on the Phoenix framework, it provides a platform for capturing and persisting questions posted to an event’s ‘question board’

▸ A question board is an online archive of questions and answers discussed at a physical or virtual event.

▸ As each question and answer submitted to a question board is persisted to the database, it is easily shared via social media.

Page 89: Build Your Own Real-Time Web Service with Elixir Phoenix

PHOENIX CHANNELS LIVE DEMO:

▸ Visit the Question Board created during the live demo

▸ Thank you to all the participants!

Page 90: Build Your Own Real-Time Web Service with Elixir Phoenix

THE ENDTHAT’S ALL FOLKS!

Page 91: Build Your Own Real-Time Web Service with Elixir Phoenix

REFERENCES & RESOURCES

▸ Elixir website http://elixir-lang.org/

▸ On Functional Elixir http://blog.plataformatec.com.br/2016/05/beyond-functional-programming-with-elixir-and-erlang/

▸ Elixir Guides http://elixir-lang.org/getting-started/introduction.html

▸ Phoenix Overview http://www.phoenixframework.org/docs/overview

▸ Erlang and Functional Programming link