KEMBAR78
Smart Gamma - Real-Time Web applications with PHP and Websocket. | PPTX
Realtime WEB applications.
Websocket usage in PHP
Стас Зозуля
Ratchet
Agenda
● What Is real-time? (for WEB)
● HTTP based techniques
● HTML5 Web sockets
● Ratchet library for PHP
● What's NEXT? (WAMP, Scaling…)
The real-time web is
“a set of technologies and practices that
enable users to receive information as soon
as it is published by its authors, rather than
requiring that they or their software check a
source periodically for updates“
https://en.wikipedia.org/wiki/Real-time_web
AJAX polling
● Latency (<= interval)
● BIG overhead
● Half-duplex
● But...
easy to implement
server
client
HTTPrequest
60s
event event
60s
HTTP Long Polling
event
TIME
● Has overhead
● Half-duplex
● Can skip events
● Low latency
HTML5 Websocket protocol
RFC 6455
“is a protocol providing full-duplex communication
channels over a single TCP connection“
https://en.wikipedia.org/wiki/WebSocket
Request
Response
Websocket
Schemes:
● ws:
● wss:Connect to websocket:
Support:
● IE 10+
● FF 6+
● Chrome 14+
● iOS 4.2+
● Android 4.4+
● Safari 5+
Ratchet PHP library
Components:
● I/O (socket transport) - IOServer
● HTTP Protocol Handler - HTTPServer
● WebSocket Protocol Handler - WsServer
● Session Provider - Symfony2 Session
● WAMP Protocol Handler - WampServer
+
● Your application
(implement MessageComponentInterface)
http://www.socketo.me/docs/design
Build on top of React. (Turns PHP into Node.js :)
http://reactphp.org/
Ratchet PHP library
$ php bin/chat-server.php
&
&
● Catch all possible exception;
● Ping a DB connection and
reconnect;
● … and everything else it
interacts with;
● Use supervisord;
● Log messages;
● Tests, tests, tests...
Code carefully
One error will ruin it all
Bundles
● Gos Web Socket Bundle
https://github.com/GeniusesOfSymfony/WebSocketBundle
● ThruwayBundle
https://github.com/voryx/ThruwayBundle
● P2RatchetBundle
https://github.com/phillies2k/ratchet-bundle
Web Application Messaging Protocol
JSON based WebSocket subprotocol that provides two application
messaging patterns in one unified protocol:
● Remote Procedure Calls (call function and get result)
● Publish & Subscribe (topics - channels in websocket)
onOpen
onClose
onCall - call a remote procedure
onSubscribe - subscribe to topic
onUnsubscribe - unsubscribe from topic
onPublish - publish to topic
onError
Events:
Client libs: http://wamp-proto.org/implementations/
http://wamp-proto.org/
Problems
1. Ratchet will not support SSL for server
Nginx, HAProxy, STunel
2. Difficult to scale (load balancers)
Reverse proxies still can help
http://www.leggetter.co.uk/real-time-web-technologies-guide/
Q & A

Smart Gamma - Real-Time Web applications with PHP and Websocket.

  • 1.
    Realtime WEB applications. Websocketusage in PHP Стас Зозуля Ratchet
  • 2.
    Agenda ● What Isreal-time? (for WEB) ● HTTP based techniques ● HTML5 Web sockets ● Ratchet library for PHP ● What's NEXT? (WAMP, Scaling…)
  • 3.
    The real-time webis “a set of technologies and practices that enable users to receive information as soon as it is published by its authors, rather than requiring that they or their software check a source periodically for updates“ https://en.wikipedia.org/wiki/Real-time_web
  • 4.
    AJAX polling ● Latency(<= interval) ● BIG overhead ● Half-duplex ● But... easy to implement
  • 5.
    server client HTTPrequest 60s event event 60s HTTP LongPolling event TIME ● Has overhead ● Half-duplex ● Can skip events ● Low latency
  • 6.
    HTML5 Websocket protocol RFC6455 “is a protocol providing full-duplex communication channels over a single TCP connection“ https://en.wikipedia.org/wiki/WebSocket Request Response
  • 7.
    Websocket Schemes: ● ws: ● wss:Connectto websocket: Support: ● IE 10+ ● FF 6+ ● Chrome 14+ ● iOS 4.2+ ● Android 4.4+ ● Safari 5+
  • 8.
    Ratchet PHP library Components: ●I/O (socket transport) - IOServer ● HTTP Protocol Handler - HTTPServer ● WebSocket Protocol Handler - WsServer ● Session Provider - Symfony2 Session ● WAMP Protocol Handler - WampServer + ● Your application (implement MessageComponentInterface) http://www.socketo.me/docs/design Build on top of React. (Turns PHP into Node.js :) http://reactphp.org/
  • 9.
    Ratchet PHP library $php bin/chat-server.php
  • 10.
  • 11.
  • 12.
    ● Catch allpossible exception; ● Ping a DB connection and reconnect; ● … and everything else it interacts with; ● Use supervisord; ● Log messages; ● Tests, tests, tests... Code carefully One error will ruin it all
  • 13.
    Bundles ● Gos WebSocket Bundle https://github.com/GeniusesOfSymfony/WebSocketBundle ● ThruwayBundle https://github.com/voryx/ThruwayBundle ● P2RatchetBundle https://github.com/phillies2k/ratchet-bundle
  • 14.
    Web Application MessagingProtocol JSON based WebSocket subprotocol that provides two application messaging patterns in one unified protocol: ● Remote Procedure Calls (call function and get result) ● Publish & Subscribe (topics - channels in websocket) onOpen onClose onCall - call a remote procedure onSubscribe - subscribe to topic onUnsubscribe - unsubscribe from topic onPublish - publish to topic onError Events: Client libs: http://wamp-proto.org/implementations/ http://wamp-proto.org/
  • 15.
    Problems 1. Ratchet willnot support SSL for server Nginx, HAProxy, STunel 2. Difficult to scale (load balancers) Reverse proxies still can help
  • 16.
  • 17.