Slide 1

Slide 1 text

Adding Real-time Features to PHP Applications

Slide 2

Slide 2 text

About me @ronnylt Ronny López Technical Lead Opinions are my own

Slide 3

Slide 3 text

Agenda • Concepts and foundations • Real-time communication patterns • Implementations • Examples

Slide 4

Slide 4 text

Real-time A system is said to be real-time if the total correctness of an operation depends not only upon its logical correctness, but also upon the time in which it is performed

Slide 5

Slide 5 text

Real-time Applications must guarantee response within specified time constraints, often referred to as “deadlines”

Slide 6

Slide 6 text

Real-time Applications in which the computer must respond as rapidly as required by the user

Slide 7

Slide 7 text

Criteria for real-time • Hard – missing a deadline is a total system failure
 • Firm – infrequent deadline misses are tolerable, but may degrade the system's QoS. Results are NOT usefulness after its deadline
 • Soft – the usefulness of a result degrades after its deadline, thereby degrading the system’s QoS

Slide 8

Slide 8 text

Real-time Soft real-time

Slide 9

Slide 9 text

Soft real-time Typically used to solve issues of concurrent access and the need to keep a number of connected systems up-to-date through changing situations

Slide 10

Slide 10 text

Soft real-time use cases • Live audio-video systems • Users collaboration • Messaging applications, etc… • Real-time analytics • Gaming • Etc…

Slide 11

Slide 11 text

The road to 500 million Symfony downloads https://symfony.com/500million

Slide 12

Slide 12 text

Why adding a soft
 real-time feature? • To improve end-user experience • Due to insufficient scaling capacity

Slide 13

Slide 13 text

Real-time Communication on the Web

Slide 14

Slide 14 text

A Bit of History • Flash • Ajax (XMLHttpRequest) • Comet (reverse Ajax) • WebSocket • Polyfills

Slide 15

Slide 15 text

The Modern Web • WebSockets • HTTP/2

Slide 16

Slide 16 text

WebSockets • Full-duplex communication channels over a single TCP connection • Currently supported in most major browsers • Can be used by any client or server application

Slide 17

Slide 17 text

HTTP/2 • Major revision of the HTTP • Replacement for how HTTP is expressed “on the wire” • Focus on performance, end-user perceived latency, network and server resource usage

Slide 18

Slide 18 text

The Mobile Internet • Inestable connections • HTTP & TCP slow-start are usually not a good match for constantly dropped connections • Network interface kills battery • Large responses + periodic polling = bad

Slide 19

Slide 19 text

What Every Web Developer Should Know About Networking and Browser Performance

Slide 20

Slide 20 text

Real-time Communication Patterns

Slide 21

Slide 21 text

The “actors” Client Server Peer Peer

Slide 22

Slide 22 text

Basic Patterns • Remote Procedure Call (RPC) • PUB/SUB

Slide 23

Slide 23 text

RPC • Allows to call a procedure (function) remotely • Involves peers of these three roles Caller Callee Dealer

Slide 24

Slide 24 text

RPC – Example 1 Something you usually do with Ajax Browser Server GetConference(123) {name:deSymfony,where: Madrid}

Slide 25

Slide 25 text

RPC – Example 2 Push data to the browser Browser Server DisplayOffer(offer)

Slide 26

Slide 26 text

RPC – Example 3 Communication between micro-services Auth Server login(user) Analytics Payments track(user) process(order)

Slide 27

Slide 27 text

Publisher/Subscriber • A peer subscribe to a topic • Another peer publish a message about this topic • All publishers interested in the topic receives the message

Slide 28

Slide 28 text

PUB/SUB – Example 1 Notifications Browser Server updateStats(stats) Browser Browser Subscribed To: StatsUpdate

Slide 29

Slide 29 text

PUB/SUB – Example 2 Micro-services Synchronization Auth Admin Service updateSettings(freshSettings) Encoding Payments Subscribed To: SettingsChanges Analytics

Slide 30

Slide 30 text

Web Application Messaging Protocol
 http://wamp-proto.org/

Slide 31

Slide 31 text

Not to be confused with WAMP: ”Windows + Apache + MySQL + PHP"

Slide 32

Slide 32 text

WAMP • Open standard WebSocket subprotocol • Provides two application messaging patterns in one unified protocol • Remote Procedure Calls • Publish & Subscribe

Slide 33

Slide 33 text

WAMP Features • Enables different technologies, processes, machines, etc… to communicate with each other, in soft real-time

Slide 34

Slide 34 text

WAMP Features • Based on modern Web standards: WebSocket, JSON and URIs • Designed with first-class support for different languages in mind (Polyglot)

Slide 35

Slide 35 text

Unified Application Routing Routing of events (for PUB/SUB) and routing of calls (for RPC) in one unified protocol Caller Callee Dealer Publisher Subscriber Broker

Slide 36

Slide 36 text

Dealer • Routes calls from the caller to the callee and routes back results or errors • Callers and callee don’t know about each other • Applications using RPC benefit from these loose coupling Caller Callee Dealer

Slide 37

Slide 37 text

Broker • Keeps a book of subscriptions • Forward the events (messages) to all subscribers • Publisher are subscribers are loosely coupled Publisher Subscriber Broker

Slide 38

Slide 38 text

Unified Protocol • When you combine a Broker and a Dealer you get what WAMP calls a Router Router Broker Dealer = +

Slide 39

Slide 39 text

The Big Picture WAMP Router (Dealer + Broker) Browser Browser Browser Browser Mobile Clients Services

Slide 40

Slide 40 text

Do we really need another wheel? How does WAMMP compare to other technologies

Slide 41

Slide 41 text

Criteria • Pub/Sub • RPC • Routed RPC (not only point-to-point) • Web native: run natively on the Web (without tunneling or bridging) • Cross Language • Open Standard

Slide 42

Slide 42 text

Tech PubSub RPC Routed RPC Web native Cross Language Open Standard WAMP ✔ ✔ ✔ ✔ ✔ ✔ Ajax ✔ ✔ ✔ Comet ✔ ✔ JSON-RPC ✔ ✔ ✔ ✔ socket.io ✔ ✔ ZMQ ✔ ✔ XMPP ✔ ✔ ✔ ✔

Slide 43

Slide 43 text

Implementations • Client libraries for most popular languages • Full featured router implementations in several languages

Slide 44

Slide 44 text

WAMP Routers • crossbar.io – Advanced, open-source, full featured, supported by the creators of WAMP
 • Your own… It’s an open protocol

Slide 45

Slide 45 text

WAMP Ecosystem • Thruway – library built in PHP that provides both a client and a router • Turnpike – router implemented in Go
 • wamp.rt – router for NodeJS
 • Erwa – router implemented in Erlang


Slide 46

Slide 46 text

Choosing an implementation Let’s talk about trade-offs

Slide 47

Slide 47 text

Is PHP suitable for soft real-time applications?

Slide 48

Slide 48 text

Is PHP the right tool for the job?

Slide 49

Slide 49 text

No.

Slide 50

Slide 50 text

No?

Slide 51

Slide 51 text

Why not?

Slide 52

Slide 52 text

Let’s use Node.js ! It’s an opportunity to deploy Erlang or Golang, or …

Slide 53

Slide 53 text

Languages War

Slide 54

Slide 54 text

Conflicts Everywhere

Slide 55

Slide 55 text

Conflicts everywhere Trade-offs everywhere

Slide 56

Slide 56 text

Trade-off • Situation that involves losing one quality or aspect of something in return for gaining another quality or aspect
 • It often implies a decision to be made with full comprehension of both the upside and downside of a particular choice

Slide 57

Slide 57 text

Is PHP the right tool for the job? There is not simple answer

Slide 58

Slide 58 text

The simpler answer I know is: “I don’t care”

Slide 59

Slide 59 text

PHP Codebase
 (Symfony, Silex, Laravel, Drupal, etc…) Clients Web, Mobile, etc… Request/Response

Slide 60

Slide 60 text

Request/Response Real-time API
 Pub/Sub, RPC, etc.. RPC

Slide 61

Slide 61 text

• A big ecosystem of thousands of useful libraries and components easily installable thanks to Composer • Very powerful template engines, ORMs, etc…

Slide 62

Slide 62 text

• We have implemented very powerful design patters in PHP coming from Java and other languages • We have several thousands of high quality code running on production • We have invested multiple hours testing, refactoring and improving the codebase

Slide 63

Slide 63 text

It’s here to stay

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

Remember Full comprehension of both the upsides and downsides of a particular choice

Slide 66

Slide 66 text

Downsides • We have to introduce a new stack to provide real-time features

Slide 67

Slide 67 text

Upsides • Possibility to introduce real-time features without deep modifications in the current codebase • No need to learn a whole new language/ stack, with the implications it has • Loosely coupled systems

Slide 68

Slide 68 text

Upsides cont… • Opens the door to write reactive, event- based, distributed architectures • Scalability is easier to achieve by distributing messages to multiple systems

Slide 69

Slide 69 text

Examples

Slide 70

Slide 70 text

The Stack • crossbar.io used as the router (dealer+broker) • PHP client gathers and publish events • Silex/Symfony backend serve the data and contains the biz logic

Slide 71

Slide 71 text

crossbar.io • Networking platform for distributed and micro-services applications • Full implementation of the WAMP protocol • Feature rich, scalable, robust and secure • It takes care of the hard parts of messaging so you can focus on your app's features

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

Tips and Tricks • WSS • Deadlines • Timeouts • Retries with back-off, etc… • Idempotency

Slide 74

Slide 74 text

Opinionated Conclusion • Understand core concepts and patterns, technology is volatile • Question everything: Why?, why not? • Decide based on several factors: user experience, scalability, feasibility, developer experience, maintenance costs/debt, etc…

Slide 75

Slide 75 text

Don’t Stop Here • gRPC – A high performance, open-source universal RPC framework from Google
 
 http://www.grpc.io/
 • IoT, WoT, etc… – real world objects connected to the wider internet

Slide 76

Slide 76 text

References • WAMP Proto – http://wamp-proto.org/ • https://github.com/voryx/ThruwayBundle • https://www.infoq.com/articles/websocket- and-http2-coexist

Slide 77

Slide 77 text

Questions • What about React PHP? • Multi-Threading in PHP with pthreads?
 https://gist.github.com/krakjoe/6437782/ • Micro-services what?

Slide 78

Slide 78 text

Gracias! @ronnylt Work with us!