Building real time applications with Symfony2

51
Building Real-Time Apps With Symfony2 Antonio Perić-Mažar, Locastic 24.10.2015 Symfony Camp Ukraine

Transcript of Building real time applications with Symfony2

Page 1: Building real time applications with Symfony2

Building Real-Time Apps With Symfony2

Antonio Perić-Mažar, Locastic 24.10.2015 Symfony Camp Ukraine

Page 2: Building real time applications with Symfony2

@antonioperic

About me

• Antonio Perić-Mažar, mag. ing. comp.

• CEO @ Locastic • Software developer, Symfony2 • Sylius Contributor :)

• www.locastic.com • [email protected] • twitter: @antonioperic

Page 3: Building real time applications with Symfony2

@antonioperic

Who we are?

• Locastic (www.locastic.com) • Web and mobile development • UI/UX design • Located in Split, Croatia

Page 4: Building real time applications with Symfony2

@antonioperic

Page 5: Building real time applications with Symfony2

@antonioperic

What is RTA?

A real-time application (RTA) is an application program that functions within a time frame that the user senses as immediate or current. The latency must be less than a defined value, usually measured in seconds. Whether or not a given application qualifies as an RTA depends on the worst-case execution time (WCET), the maximum length of time a defined task or set of tasks requires on a given hardware platform. The use of RTAs is called real-time computing (RTC).

Page 6: Building real time applications with Symfony2

@antonioperic

Page 7: Building real time applications with Symfony2

@antonioperic

Use Cases

• Videoconference applications, VoIP • Data Visualisations, Location tracking • Online gaming, multi-user collaboration • Community storage solutions • Some e-commerce transactions • Chatting, IM (instant messaging) • Notifications & Signalling • Activity Streams • …

Page 8: Building real time applications with Symfony2

@antonioperic

Use Cases

Real-Time == UX

etc.

Page 9: Building real time applications with Symfony2

@antonioperic

Understanding RTA

• History of Realtime • LiveConnect • Http Based solutions • Comet

Page 10: Building real time applications with Symfony2

@antonioperic

HTTP

• A client requests a webpage from a server. • The server calculates the response • The server sends the response to the

client.

Page 11: Building real time applications with Symfony2

@antonioperic

Webhooks

•server only•low update rates •uni-directional - from publisher to consumer

(server to server)•avg. latency•high. efficiency (for servers)

Page 12: Building real time applications with Symfony2

@antonioperic

Polling

Page 13: Building real time applications with Symfony2

@antonioperic

Polling

• A client requests a webpage from a server using regular HTTP (see HTTP above).

• The requested webpage executes JavaScript which requests a file from the server at regular intervals (e.g. 0.5 seconds).

• The server calculates each response and sends it back, just like normal HTTP traffic.

Page 14: Building real time applications with Symfony2

@antonioperic

Polling

• server or client • low update rates • batched bi-directional • high latency • low efficiency

Page 15: Building real time applications with Symfony2

@antonioperic

Long Polling

Page 16: Building real time applications with Symfony2

@antonioperic

Long Polling• A client requests a webpage from a server using regular HTTP

(see HTTP above). • The requested webpage executes JavaScript which requests a

file from the server. • The server does not immediately respond with the requested

information but waits until there's new information available.

• When there's new information available, the server responds with the new information.

• The client receives the new information and immediately sends another request to the server, re-starting the process.

Page 17: Building real time applications with Symfony2

@antonioperic

Long Polling

• server or client • avg. update rates • batched bi-directional • avg. latency • avg. efficiency

Page 18: Building real time applications with Symfony2

@antonioperic

HTML5 Server Sent Events (SSE) / EventSource

Page 19: Building real time applications with Symfony2

@antonioperic

HTML5 Server Sent Events (SSE) / EventSource• A client requests a webpage from a server using regular

HTTP • The requested webpage executes javascript which

opens a connection to the server. • The server sends an event to the client when there's

new information available. • Real-time traffic from server to client, mostly that's

what you'll need • You'll want to use a server that has an event loop • Not possible to connect with a server from another

domain

Page 20: Building real time applications with Symfony2

@antonioperic

HTML5 Server Sent Events (SSE) / EventSource• client (pushing) • good update rates • unidirectional • good latency • good efficiency

Page 21: Building real time applications with Symfony2

@antonioperic

WebSockets

Page 22: Building real time applications with Symfony2

@antonioperic

WebSockets• A client requests a webpage from a server using regular http (see

HTTP above). • The requested webpage executes JavaScript which opens a connection

with the server. • The server and the client can now send each other messages when

new data (on either side) is available. • Real-time traffic from the server to the client and from the client

to the server • You'll want to use a server that has an event • With WebSockets it is possible to connect with a server from

another domain. • It is also possible to use a third party hosted websocket server, for

example Pusher or others. This way you'll only have to implement the client side, which is very easy!

Page 23: Building real time applications with Symfony2

@antonioperic

WebSockets

• TCP, fully duplex single connection • server or client (client mostly) • bi-directional • high update rates • low latency • high efficiency

Page 24: Building real time applications with Symfony2

@antonioperic

Streaming

• make connection, keep it open, as soon as we have updates push it to client

• server or client • high updated rates • uni-directional • low latency • high efficiency • business decisions, trading, unidirectional you can

only push it from server to client, single persisting connection 

• look as servers and event as standard

Page 25: Building real time applications with Symfony2

@antonioperic

WebRTC

• server or client (normally client2client)• high update rates• bi-directional• low latency• high efficiency• audio or video generally peer2peer

Page 26: Building real time applications with Symfony2

@antonioperic

Communication patterns

• Simple messaging • Pub/Sub • RPC/RMI • Data Sync

Page 27: Building real time applications with Symfony2

@antonioperic

Simple Messaging

• No abstraction. Just messages • Fits well with HTTP and REST • Many persist connections

Page 28: Building real time applications with Symfony2

@antonioperic

Pub/Sub

• Subscribe and publish on channels • Multiplexed. Single connection. • Clearity partitions complex data • Dedicated Endpoint • Required SDK

Page 29: Building real time applications with Symfony2

@antonioperic

RPC/RMI

• Call API Methods • SDK Required • Hides network activity from developer

Page 30: Building real time applications with Symfony2

@antonioperic

Data Sync

• Interact with, and synchronise, data structures

• Dedicated endpoint + Protocol • New Paradigm • SDK Required • Hides network activity from developer

Page 31: Building real time applications with Symfony2

@antonioperic

How to pick technology?

• First: Are the only real time solutions in NodeJS?

Page 32: Building real time applications with Symfony2

@antonioperic

How to pick technology?

• First: Are the only real time solutions in NodeJS?

NO!

Page 33: Building real time applications with Symfony2

@antonioperic

Solutions by language

• PHP: ReactPHP, Ratchet, dNode-php, phpDaemon• Java: Netty, Jetty• JavaScript (Node.JS): Faye, Socket.IO (Engine.IO), Primus.io

• .NET (C#): SignalR, XSockets• Python: Lots of options built on Tornado• Ruby: em-websocket, Faye• Language agnostic: most hosted services

Page 34: Building real time applications with Symfony2

@antonioperic

Solutions by language

j.mp/realtime-tech-guide

Page 35: Building real time applications with Symfony2

@antonioperic

PHP?

• Apache is not built for Real-Time • PHP is not build for Real-Time • Real-Time is not impossible with PHP

Page 36: Building real time applications with Symfony2

@antonioperic

PHP RTA

• Self hosted PHP solution • Self hosted Side-by-Side solution • Hosted solution

Page 37: Building real time applications with Symfony2

@antonioperic

Self hosted PHP

• tightly coupled • two stacks:

• sync stack • async stack

Page 38: Building real time applications with Symfony2

@antonioperic

Page 39: Building real time applications with Symfony2

@antonioperic

PHP Self Hosted solutions

• React (PHP)• Event-driven, non-blocking I/O with PHP.

• Ratchet (Built on React PHP)• WebSockets, WAMP, PubSub samples. No HTTP Fallback

• dnode-php (RPC/RMI)• phpDaemon

• Lots of examples. Most docs in Russian.

Page 40: Building real time applications with Symfony2

@antonioperic

Page 41: Building real time applications with Symfony2

@antonioperic

PHP Self Hosted solutions

Pros:

• PHP• Simple integration• Standards-based

• WAMP/Autobahn• JS, Android, iOS & more

Cons:

• No HTTP fallback• Low-level abstractions• Different programming style

• You need to scale

Page 42: Building real time applications with Symfony2

@antonioperic

Page 43: Building real time applications with Symfony2

@antonioperic

PHP + Faye Self Hosted solutionsPros:

• PubSub• Connection fallback• Redis support is build in• Simple integration

Cons:

• Not PHP?• You need to scale

Page 44: Building real time applications with Symfony2

@antonioperic

Other side by side solutions

• Socket.IO • SockJS • SignalR • see real time tech guide for more

Page 45: Building real time applications with Symfony2

@antonioperic

Page 46: Building real time applications with Symfony2

@antonioperic

PHP + Hosted solutions

Pros:

• Simple and powerful• Instantly scalable • Managed & Dedicated• Direct integration into Symfony

Cons:

• 3rd party reliance

Page 47: Building real time applications with Symfony2

@antonioperic

What we build?

• Real time dashboards ford Google Adwords analytics

• Symfony2, AngularJS, RabbitMQ • First version was with Polling • Second version moving to hosted solution

- Pusher

Page 48: Building real time applications with Symfony2

@antonioperic

Other hosted solutions

• PubNub • Realtime.co • Firebase • Simperium • see real time tech guide for more

Page 49: Building real time applications with Symfony2

@antonioperic

References

• Phil @Leggetter - Head of Developer Relations at @pusher. Realtime Web Technology Evangelist.

• Real-time Tech Guide• React (PHP)• Ratchet (PHP)• Faye (Node/Ruby)• Pusher• LopiPusherBundle• github.com/leggetter/realtime-symfonyexamples

Page 50: Building real time applications with Symfony2

@antonioperic

Thank you!

Page 51: Building real time applications with Symfony2

@antonioperic

QA

follow me on twitter: @antonioperic