Adding Real-time Features to PHP Applications
-
Upload
ronny-lopez -
Category
Software
-
view
210 -
download
1
Transcript of Adding Real-time Features to PHP Applications
![Page 1: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/1.jpg)
Adding Real-time Features to PHP
Applications
![Page 2: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/2.jpg)
About me
@ronnylt
Ronny López
Technical Lead
Opinions are my own
![Page 3: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/3.jpg)
Agenda
• Concepts and foundations
• Real-time communication patterns
• Implementations
• Examples
![Page 4: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/4.jpg)
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
![Page 5: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/5.jpg)
Real-time
Applications must guarantee response within specified time constraints, often referred to as “deadlines”
![Page 6: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/6.jpg)
Real-time
Applications in which the computer must respond as rapidly as required by the user
![Page 7: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/7.jpg)
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
![Page 8: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/8.jpg)
Real-timeSoft real-time
![Page 9: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/9.jpg)
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
![Page 10: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/10.jpg)
Soft real-time use cases
• Live audio-video systems
• Users collaboration
• Messaging applications, etc…
• Real-time analytics
• Gaming
• Etc…
![Page 11: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/11.jpg)
The road to 500 million Symfony downloads
https://symfony.com/500million
![Page 12: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/12.jpg)
Why adding a soft real-time feature?
• To improve end-user experience
• Due to insufficient scaling capacity
![Page 13: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/13.jpg)
Real-time Communication on the
Web
![Page 14: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/14.jpg)
A Bit of History
• Flash
• Ajax (XMLHttpRequest)
• Comet (reverse Ajax)
• WebSocket
• Polyfills
![Page 15: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/15.jpg)
The Modern Web
• WebSockets
• HTTP/2
![Page 16: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/16.jpg)
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
![Page 17: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/17.jpg)
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
![Page 18: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/18.jpg)
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
![Page 19: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/19.jpg)
What Every Web Developer Should Know About Networking and Browser Performance
![Page 20: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/20.jpg)
Real-time Communication
Patterns
![Page 21: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/21.jpg)
The “actors”
Client Server
Peer Peer
![Page 22: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/22.jpg)
Basic Patterns
• Remote Procedure Call (RPC)
• PUB/SUB
![Page 23: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/23.jpg)
RPC
• Allows to call a procedure (function) remotely
• Involves peers of these three roles
Caller CalleeDealer
![Page 24: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/24.jpg)
RPC – Example 1Something you usually do with Ajax
Browser Server
GetConference(123)
{name:deSymfony,where: Madrid}
![Page 25: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/25.jpg)
RPC – Example 2Push data to the browser
BrowserServerDisplayOffer(offer)
![Page 26: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/26.jpg)
RPC – Example 3Communication between micro-services
Auth
Server
login(user)
Analytics Payments
track(user) process(order)
![Page 27: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/27.jpg)
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
![Page 28: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/28.jpg)
PUB/SUB – Example 1Notifications
Browser
Server
updateStats(stats)
Browser Browser
Subscribed To: StatsUpdate
![Page 29: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/29.jpg)
PUB/SUB – Example 2Micro-services Synchronization
Auth
Admin Service
updateSettings(freshSettings)
Encoding Payments
Subscribed To: SettingsChanges
Analytics
![Page 30: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/30.jpg)
Web Application Messaging Protocol
http://wamp-proto.org/
![Page 31: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/31.jpg)
Not to be confused with WAMP: ”Windows + Apache + MySQL + PHP"
![Page 32: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/32.jpg)
WAMP
• Open standard WebSocket subprotocol
• Provides two application messaging patterns in one unified protocol
• Remote Procedure Calls
• Publish & Subscribe
![Page 33: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/33.jpg)
WAMP Features
• Enables different technologies, processes, machines, etc… to communicate with each other, in soft real-time
![Page 34: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/34.jpg)
WAMP Features
• Based on modern Web standards: WebSocket, JSON and URIs
• Designed with first-class support for different languages in mind (Polyglot)
![Page 35: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/35.jpg)
Unified Application Routing
Routing of events (for PUB/SUB) and routing of calls (for RPC) in one unified protocol
Caller CalleeDealer
Publisher SubscriberBroker
![Page 36: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/36.jpg)
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 CalleeDealer
![Page 37: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/37.jpg)
Broker
• Keeps a book of subscriptions
• Forward the events (messages) to all subscribers
• Publisher are subscribers are loosely coupled
Publisher SubscriberBroker
![Page 38: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/38.jpg)
Unified Protocol
• When you combine a Broker and a Dealer you get what WAMP calls a Router
Router Broker Dealer= +
![Page 39: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/39.jpg)
The Big Picture
WAMP Router(Dealer + Broker)
BrowserBrowserBrowserBrowser
Mobile Clients
Services
![Page 40: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/40.jpg)
Do we really need another wheel?
How does WAMMP compare to other technologies
![Page 41: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/41.jpg)
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
![Page 42: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/42.jpg)
Tech PubSub RPC RoutedRPC Web native Cross
LanguageOpen
Standard
WAMP ✔ ✔ ✔ ✔ ✔ ✔
Ajax ✔ ✔ ✔
Comet ✔ ✔
JSON-RPC ✔ ✔ ✔ ✔
socket.io ✔ ✔
ZMQ ✔ ✔
XMPP ✔ ✔ ✔ ✔
![Page 43: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/43.jpg)
Implementations
• Client libraries for most popular languages
• Full featured router implementations in several languages
![Page 44: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/44.jpg)
WAMP Routers
• crossbar.io – Advanced, open-source, full featured, supported by the creators of WAMP
• Your own… It’s an open protocol
![Page 45: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/45.jpg)
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
![Page 46: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/46.jpg)
Choosing an implementation
Let’s talk about trade-offs
![Page 47: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/47.jpg)
Is PHP suitable for soft real-time applications?
![Page 48: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/48.jpg)
Is PHP the right tool for the job?
![Page 49: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/49.jpg)
No.
![Page 50: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/50.jpg)
No?
![Page 51: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/51.jpg)
Why not?
![Page 52: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/52.jpg)
Let’s use Node.js ! It’s an opportunity to
deploy Erlang or Golang, or …
![Page 53: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/53.jpg)
Languages War
![Page 54: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/54.jpg)
Conflicts Everywhere
![Page 55: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/55.jpg)
Conflicts everywhereTrade-offs everywhere
![Page 56: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/56.jpg)
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
![Page 57: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/57.jpg)
Is PHP the right tool for the job?There is not simple answer
![Page 58: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/58.jpg)
The simpler answer I know is:“I don’t care”
![Page 59: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/59.jpg)
PHP Codebase(Symfony, Silex, Laravel, Drupal, etc…)
ClientsWeb, Mobile, etc…
Request/Response
![Page 60: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/60.jpg)
Request/Response
Real-time API Pub/Sub, RPC, etc..
RPC
![Page 61: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/61.jpg)
• A big ecosystem of thousands of useful libraries and components easily installable thanks to Composer
• Very powerful template engines, ORMs, etc…
![Page 62: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/62.jpg)
• 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
![Page 63: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/63.jpg)
It’s here to stay
![Page 64: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/64.jpg)
![Page 65: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/65.jpg)
RememberFull comprehension of both the upsides and downsides
of a particular choice
![Page 66: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/66.jpg)
Downsides
• We have to introduce a new stack to provide real-time features
![Page 67: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/67.jpg)
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
![Page 68: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/68.jpg)
Upsides cont…
• Opens the door to write reactive, event-based, distributed architectures
• Scalability is easier to achieve by distributing messages to multiple systems
![Page 69: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/69.jpg)
Examples
![Page 70: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/70.jpg)
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
![Page 71: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/71.jpg)
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
![Page 72: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/72.jpg)
![Page 73: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/73.jpg)
Tips and Tricks
• WSS
• Deadlines
• Timeouts
• Retries with back-off, etc…
• Idempotency
![Page 74: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/74.jpg)
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…
![Page 75: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/75.jpg)
Don’t Stop Here
• gRPC – A high performance, open-source universal RPC framework from Googlehttp://www.grpc.io/
• IoT, WoT, etc… – real world objects connected to the wider internet
![Page 76: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/76.jpg)
References
• WAMP Proto – http://wamp-proto.org/
• https://github.com/voryx/ThruwayBundle
• https://www.infoq.com/articles/websocket-and-http2-coexist
![Page 77: Adding Real-time Features to PHP Applications](https://reader034.fdocuments.in/reader034/viewer/2022042520/586f8a331a28ab54768b62f5/html5/thumbnails/77.jpg)
Questions
• What about React PHP?
• Multi-Threading in PHP with pthreads? https://gist.github.com/krakjoe/6437782/
• Micro-services what?