adding real-time features to php applications

Download Adding Real-time Features to PHP Applications

Post on 16-Apr-2017




1 download

Embed Size (px)


  • Adding Real-time Features to PHP


  • About me


    Ronny Lpez

    Technical Lead

    Opinions are my own

  • Agenda

    Concepts and foundations Real-time communication patterns Implementations Examples

  • 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

  • Real-time

    Applications must guarantee response within specified time constraints, often referred to as deadlines

  • Real-time

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

  • 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 systems QoS

  • Real-timeSoft real-time

  • 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

  • Soft real-time use cases

    Live audio-video systems Users collaboration Messaging applications, etc Real-time analytics Gaming Etc

  • The road to 500 million Symfony downloads

  • Why adding a soft real-time feature?

    To improve end-user experience Due to insufficient scaling capacity

  • Real-time Communication on the


  • A Bit of History

    Flash Ajax (XMLHttpRequest) Comet (reverse Ajax) WebSocket Polyfills

  • The Modern Web

    WebSockets HTTP/2

  • 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

  • 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

  • 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

  • What Every Web Developer Should Know About Networking and Browser Performance

  • Real-time Communication


  • The actors

    Client Server

    Peer Peer

  • Basic Patterns

    Remote Procedure Call (RPC) PUB/SUB

  • RPC

    Allows to call a procedure (function) remotely

    Involves peers of these three roles

    Caller CalleeDealer

  • RPC Example 1Something you usually do with Ajax

    Browser Server


    {name:deSymfony,where: Madrid}

  • RPC Example 2Push data to the browser


  • RPC Example 3Communication between micro-services




    Analytics Payments

    track(user) process(order)

  • Publisher/Subscriber

    A peer subscribe to a topic Another peer publish a message about this


    All publishers interested in the topic receives the message

  • PUB/SUB Example 1Notifications




    Browser Browser

    Subscribed To: StatsUpdate

  • PUB/SUB Example 2Micro-services Synchronization


    Admin Service


    Encoding Payments

    Subscribed To: SettingsChanges


  • Web Application Messaging Protocol

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

  • WAMP

    Open standard WebSocket subprotocol Provides two application messaging

    patterns in one unified protocol

    Remote Procedure Calls Publish & Subscribe

  • WAMP Features

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

  • WAMP Features

    Based on modern Web standards: WebSocket, JSON and URIs

    Designed with first-class support for different languages in mind (Polyglot)

  • Unified Application Routing

    Routing of events (for PUB/SUB) and routing of calls (for RPC) in one unified protocol

    Caller CalleeDealer

    Publisher SubscriberBroker

  • Dealer

    Routes calls from the caller to the callee and routes back results or errors

    Callers and callee dont know about each other

    Applications using RPC benefit from these loose coupling

    Caller CalleeDealer

  • Broker

    Keeps a book of subscriptions Forward the events (messages) to all


    Publisher are subscribers are loosely coupled

    Publisher SubscriberBroker

  • Unified Protocol

    When you combine a Broker and a Dealer you get what WAMP calls a Router

    Router Broker Dealer= +

  • The Big Picture

    WAMP Router(Dealer + Broker)


    Mobile Clients


  • Do we really need another wheel?

    How does WAMMP compare to other technologies

  • 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

  • Tech PubSub RPC RoutedRPC Web nativeCross









  • Implementations

    Client libraries for most popular languages Full featured router implementations in

    several languages

  • WAMP Routers Advanced, open-source, full featured, supported by the creators of WAMP

    Your own Its an open protocol

  • 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

  • Choosing an implementation

    Lets talk about trade-offs

  • Is PHP suitable for soft real-time applications?

  • Is PHP the right tool for the job?

  • No.

  • No?

  • Why not?

  • Lets use Node.js ! Its an opportunity to

    deploy Erlang or Golang, or

  • Languages War

  • Conflicts Everywhere

  • Conflicts everywhereTrade-offs everywhere

  • 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

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

  • The simpler answer I know is:I dont care

  • PHP Codebase(Symfony, Silex, Laravel, Drupal, etc)

    ClientsWeb, Mobile, etc


  • Request/Response

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


  • A big ecosystem of thousands of useful libraries and components easily installable thanks to Composer

    Very powerful template engines, ORMs, etc

  • 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

  • Its here to stay

  • RememberFull comprehension of both the upsides and downsides

    of a particular choice

  • Downsides

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

  • 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

  • Upsides cont

    Opens the door to write reactive, event-based, distributed architectures

    Scalability is easier to achieve by distributing messages to multiple systems

  • Examples

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

    contains the biz logic


    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

  • Tips and Tricks

    WSS Deadlines Timeouts Retries with back-off, etc Idempotency

  • 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

  • Dont Stop Here

    gRPC A high performance, open-source universal RPC framework from Google

    IoT, WoT, etc real world objects


View more >