SlideShare a Scribd company logo
Realtime Communication Techniques with PHP Scott Mattocks & Chris Lewis, OnForce, Inc.
Agenda Introductions Overview User expectations Problems with delivery Techniques Refresh Short Polling Long Polling WebSockets Q&A
Introductions Scott Mattocks Senior PHP developer at OnForce, Inc. Contributor to PEAR Author of Pro PHP-GTK Chris Lewis Senior PHP developer at OnForce, Inc. 6 years of enterprise PHP experience  12 years of front-end web development experience Both are contributors to the WaterSpout Real-time Communication Server
Overview Example:  http://www.spoutserver.com Click “John Locke” in the Demos section
Overview Users expect data to be delivered more quickly and seemlessly Gmail changed the way user expect to interact with websites Loading an entire page to show a small change is slow and wasteful
Problems with delivery The web runs (for the most part) on HTTP HTTP is one way User asks for data Server sends data back The server can't do anything without the user asking first
Problems with delivery Timeliness There is a gap between arrival of the data on the server and notification of the user. We want to reduce the gap without killing the servers. Client Server New Data New Data Black out
Problems with delivery Efficiency How much of the transfered data is important to the user? Data transfer: X bytes sent for request (GET headers) Y bytes sent for response (body and headers) Z bytes of new data Z / (X + Y) = efficiency We want try to send only the data that has changed and that is important to the user
Problems with delivery Scalability What kind of load are we causing on the server? Are we holding resources (i.e. database connections) that could be used to server other users? We want to do more with less
Solutions Refresh the page Short Polling Long Polling 1 server 2 servers WebSockets
Solutions @gnomeboy want to come over for some cookies? @gnomegirl hellz yeah! Example: Simple twitter feed style page
Solutions Page consists of a wrapper around message content Avg message size = 100 bytes Avg page size (without images) = 4 KB Avg request headers = 410 bytes Avg response headers = 210 bytes (JavaScript code examples use Prototype)
Refresh The same as if the user hit  F5 Reloads the entire page New page load adds the new message No constant connection to the server Black out periods between requests
Refresh database web server web server
Refresh Difficulty 1 out of 10 Use HTML or JavaScript <meta http-equiv=&quot;refresh&quot; content=&quot;5&quot; /> setTimeout('window.location.reload(true)', 5000);
Refresh Timeliness Timeliness of data depends on the refresh rate There is an additional delay for the round trip to the server Includes time to send request, process request, and send response
Refresh Efficiency (assume 10 total requests, 1 new message) Data transfer: 410 bytes sent for each request 4210 bytes sent for each response, 4310 for new message response 100 bytes of new data 100 / ((410 + 4210) * 10 + 100) = .0021 per message Server resources used 1 HTTP process per request (10 total) 1 Database connection per request (10 total)
Short Polling Ask the server for updates since the last time you asked Like a road trip with a 3 year old Are we there yet? Are we there yet? No constant connection to the server Black out periods between requests Cursor required to prevent data loss
Short Polling database web server web server
Short Polling Difficulty 5 out of 10 AJAX Background request to the server checks for new messages at a timed interval New messages are added to DOM by JavaScript
Short Polling function   poll_server () { new  Ajax. Request ( this .urlPolling, { method:  'post' , parameters: data, onSuccess:  this .successHandler, onFailure:  this .handleFailure, onException:  this .handleException }); } setInterval ( 'poll_server()' ,  5000 );
Short Polling function   successHandler (trans, messages_div) { if  (trans.responseText) { var  json = trans.responseText. evalJSON (); if  (json.message) { var  msg = json.message +  '<br />' ; $(messages_div). insert (msg); } } }
Short Polling function   successHandler (trans, messages_div) { if  (trans.responseText) { var  json = trans.responseText. evalJSON (); if  (json.message) { var  msg = json.message +  '<br />' ; $(messages_div). insert (msg); } } }
Short Polling Timeliness Timeliness of data depends on the refresh rate There is an additional delay for the round trip to the server Includes time to send request, process request, and send response
Short Polling Efficiency (assume 10 total requests, 1 new message) Data transfer: 410 bytes sent for each request 210 bytes sent for each empty response, 310 for new message response 100 bytes of new data 100 / ((410 + 210) * 10 + 100) = .0158 per message Server resources used 1 HTTP process per request (10 total) 1 Database connection per request (10 total)
Long Polling - 1 server Ask the server for updates since the last time you asked Server does not respond until a new message has arrived No constant connection to the server Black out periods between sending response and next request Cursor required to prevent data loss
Long Polling – 1 server database web server web server
Long Polling – 1 server Difficulty – Client side 5 out of 10 AJAX Background request to the server checks for new messages and waits for responses New messages are added to DOM by JavaScript
Long Polling – 1 server Difficulty – Server side 5 out of 10 Continuously loop checking for new messages Break out of loop when new message arrives
Long Polling – 1 server function   poll_server () { new  Ajax. Request ( this .urlPolling, { method:  'post' , parameters: data, onSuccess:  this .successHandler, onFailure:  this .handleFailure, onException:  this .handleException }); } document. observe ( 'dom:loaded' , poll_server);
Long Polling – 1 server function   successHandler (trans, messages_div) { if  (trans.responseText) { var  json = trans.responseText. evalJSON (); if  (json.message) { var  msg = json.message +  '<br />' ; $(messages_div). insert (msg); } }   poll_server (); }
Long Polling – 1 server <?php $query  =  'SELECT message FROM messages WHERE dateadded > ?' ; $stmt   =  $pdo -> prepare ( $query ); while  (true) { $message  =  $stmt -> execute ( $cursor_date ); if  (! empty ( $message )) { echo   json_encode ( $message ); break ; } } ?>
Long Polling – 1 server Timeliness Near real-time As soon as a message shows up on the server, it is sent to the waiting client Black out period between sending response and making new request can add some delay
Long Polling – 1 server Efficiency (1 request, 1 new message) Data transfer: 410 bytes sent for request 310 bytes for new message response 100 bytes of new data 100 / (410 + 310) = .138 per message Server resources used 1 HTTP process 1 Database connection
Long Polling – 2 servers 2 servers work together to use fewer resources Main web server handles initial page request Main server informs polling server when new message arrives Polling server informs client No constant connection to server Black out periods between sending response and next request Cursor required to prevent data loss
Long Polling – 2 servers web server polling server HTTP XHR over HTTP
Long Polling – 2 servers Difficulty – Client side 5 out of 10 AJAX Background request to the server checks for new messages and waits for responses New messages are added to DOM by JavaScript
Long Polling – 2 servers Difficulty – Server side 8 out of 10 Web server makes cURL call to polling server when a new message is added Polling server determines who message is for and sends the response to the waiting connection
Long Polling – 2 servers function   poll_server () { new  Ajax. Request ( this .urlPolling, { method:  'post' , parameters: data, onSuccess:  this .successHandler, onFailure:  this .handleFailure, onException:  this .handleException }); } document. observe ( 'dom:loaded' , poll_server);
Long Polling – 2 servers function   successHandler (trans, messages_div) { if  (trans.responseText) { var  json = trans.responseText. evalJSON (); if  (json.message) { var  msg = json.message +  '<br />' ; $(messages_div). insert (msg); } }   poll_server (); }
Long Polling – 2 servers Main Web Server Polling Server <?php $query  =  'INSERT INTO messages VALUES (?, ?)' ; $stmt   =  $pdo -> prepare ( $query ); $stmt -> prepare ( $message ,  $date ); $polling_server -> send ( $message ); ?> <?php foreach  ( $this ->waiting  as   $connection ){ $connection -> respond ( $message ); } ?>
Long Polling – 2 servers Timeliness Near real-time As soon as a message shows up on the server, it is sent to the waiting client Black out period between sending response and making new request can add some delay
Long Polling – 2 servers Efficiency (1 request, 1 new message) Data transfer: 410 bytes sent for request 310 bytes for new message response 100 bytes of new data 100 / (410 + 310) = .138 per message Server resources used 1 HTTP process on polling server per message 0 Database connections
WebSockets According to http://en.wikipedia.org/wiki/WebSocket: „ WebSockets is a technology providing for bi-directional, full-duplex communications channels, over a single Transmission Control Protocol (TCP) socket, designed to be implemented in web browsers and web servers”
WebSockets WebSockets allow server to push data to client Connection established via client-initiated handshake After handshake, both the client and the server can send and recieve data Server can send data without the client asking for it first
WebSockets Builds off of 2 server long polling Main web server communicates with event server Constant connection between client and server No black out periods No need for cursor
WebSockets web server WebSocket server JSON via WebSockets
WebSockets Difficulty – Client side 5 out of 10 WebSocket API Connection established onMessage events fired when new data comes in from the server New messages are added to DOM by JavaScript
WebSockets Difficulty – Server side 8 out of 10 Web server makes cURL call to WebSocket server when a new message is added WebSocket server determines who message is for and sends the response to the waiting connection
WebSockets function   poll_server () { var  socket =  new   WebSocket ( 'ws://example.com/' ); socket.onmessage =  function  (response) { successHandler (response,  'message_errors' ); } } document. observe ( 'dom:loaded' , poll_server);
WebSockets function   successHandler (trans, messages_div) { if  (trans.data) { var  json = trans.data. evalJSON (); if  (json.message) { var  msg = json.message +  '<br />' ; $(messages_div). insert (msg); } } }
WebSockets Main Web Server WebSocket Server <?php $query  =  'INSERT INTO messages VALUES (?, ?)' ; $stmt   =  $pdo -> prepare ( $query ); $stmt -> prepare ( $message ,  $date ); $polling_server -> send ( $message ); ?> <?php foreach  ( $this ->waiting  as   $connection ){ $connection -> respond ( $message ); } ?>
WebSockets Timeliness Real-time As soon as a message shows up on the server, it is sent to the waiting client No black out period
WebSockets Post Handshake Efficiency  Data transfer: 0 bytes sent for request 100 bytes for new message response 100 bytes of new data 100 / 100 = 1.000 per message Server resources used 1 process on WebSocket server 0 Database connections
WebSockets Efficiency Demo http://spoutserver.com/demos/compare Comparison Refresh: .0021 Short Polling: .0158 Long Polling: .138 WebSockets: 1.00
WaterSpout Lightweight HTTP server Static files Dynamic content (PHP) Best used as part of event-driven server pair Can function as both ends Handles WebSockets and long polling Written in PHP
WaterSpout Two types of connections Listeners Updates When an update comes in the appropriate listeners are notified Custom controllers define which listeners should be notified
WaterSpout - Listeners <?php public function   listen () { // Handle cursor for long polling fall back. // ... $this ->dispatcher-> add_listener ( $this );  } ?>
WaterSpout - Dispatcher <?php /* Called every .25 seconds on all waiting listeners */ public function   process_event (Controller  $mover  = null) { $key  =  array_search (( int )  $this ->_cursor,  array_keys (self:: $_commands )); if  ( $key  === false && ! is_null ( $this ->_cursor)) { return ; } $commands  =  array_slice (self:: $_commands ,  $key ); if  ( empty ( $commands )) { return ; } $response  =  new   HTTPResponse ( 200 ); $body   =  array ( '__URI__'   =>  $this ->uri,  'commands'  =>  $commands, 'cursor'   =>  end ( array_keys (self:: $_commands )) +  1 ); $response -> set_body ( $body , true); $this -> write ( $response ); $this ->_cursor = ( int )  end ( array_keys (self:: $_commands )) +  1 ; } ?>
WaterSpout vs Apache/Nginx Neither Apache nor Nginx has a way for one process to notify the listeners You need to put the incoming update into some shared location (mysql, memcache, etc) Listeners have to poll the shared location  Heavy Slows down timeliness WaterSpout solves this by letting updates notify listeners
WebSockets Resources http://dev.w3.org/html5/websockets/ http://en.wikipedia.org/wiki/Web_Sockets http://www.spoutserver.com/
Questions? Contact [email_address] Twitter @spoutserver Feedback http://joind.in/talk/view/1748 Slides http://www.slideshare.net/spoutserver

More Related Content

PPTX
Socket programming with php
PPTX
Writing and using php streams and sockets
PDF
Alternative Infrastucture
ODP
PHP: The Beginning and the Zend
PDF
Final opensource record 2019
PDF
Php through the eyes of a hoster phpbnl11
PDF
Haproxy - zastosowania
Socket programming with php
Writing and using php streams and sockets
Alternative Infrastucture
PHP: The Beginning and the Zend
Final opensource record 2019
Php through the eyes of a hoster phpbnl11
Haproxy - zastosowania

What's hot (20)

PDF
Nginx وب سروری برای تمام فصول
PDF
Ngrep commands
KEY
PDF
Rihards Olups - Encrypting Daemon Traffic With Zabbix 3.0
PDF
Teach your (micro)services talk Protocol Buffers with gRPC.
PDF
PM : code faster
PDF
From zero to almost rails in about a million slides...
PPSX
Bleeding secrets
PPTX
Web server working
PDF
[Ruxcon Monthly Sydney 2011] Proprietary Protocols Reverse Engineering : Rese...
PDF
NginX - good practices, tips and advanced techniques
PDF
Linux kernel TLS и HTTPS / Александр Крижановский (Tempesta Technologies)
PDF
From zero to hero - Easy log centralization with Logstash and Elasticsearch
ODP
MNPHP Scalable Architecture 101 - Feb 3 2011
PDF
Relayd: a load balancer for OpenBSD
PPT
Why and How Powershell will rule the Command Line - Barcamp LA 4
PDF
Locker: distributed consistent locking
PDF
Nginx + PHP
TXT
!!! Huong dan !!!
Nginx وب سروری برای تمام فصول
Ngrep commands
Rihards Olups - Encrypting Daemon Traffic With Zabbix 3.0
Teach your (micro)services talk Protocol Buffers with gRPC.
PM : code faster
From zero to almost rails in about a million slides...
Bleeding secrets
Web server working
[Ruxcon Monthly Sydney 2011] Proprietary Protocols Reverse Engineering : Rese...
NginX - good practices, tips and advanced techniques
Linux kernel TLS и HTTPS / Александр Крижановский (Tempesta Technologies)
From zero to hero - Easy log centralization with Logstash and Elasticsearch
MNPHP Scalable Architecture 101 - Feb 3 2011
Relayd: a load balancer for OpenBSD
Why and How Powershell will rule the Command Line - Barcamp LA 4
Locker: distributed consistent locking
Nginx + PHP
!!! Huong dan !!!
Ad

Viewers also liked (13)

PDF
Asynchronous PHP and Real-time Messaging
PDF
Open a window, see the clouds - php|tek 2011
PPTX
Aplicaciones en tiempo real con nodejs y html5
PPT
App en tiempo real con HTML5+Node.js+Socket.IO
PPTX
Responsive Web Design
PPT
5 Tornados Intro
PPTX
Waterspout
PPTX
TORNADOS
PPTX
Water slideshare
PDF
Angular workflow with gulp.js
PDF
Hybrid Apps with Angular & Ionic Framework
PPTX
Water presentation
PPT
Water Resources Power Point Presentation
Asynchronous PHP and Real-time Messaging
Open a window, see the clouds - php|tek 2011
Aplicaciones en tiempo real con nodejs y html5
App en tiempo real con HTML5+Node.js+Socket.IO
Responsive Web Design
5 Tornados Intro
Waterspout
TORNADOS
Water slideshare
Angular workflow with gulp.js
Hybrid Apps with Angular & Ionic Framework
Water presentation
Water Resources Power Point Presentation
Ad

Similar to Realtime Communication Techniques with PHP (20)

PPTX
Taking a Quantum Leap with Html 5 WebSocket
PPTX
Module 5.pptx HTTP protocol on optical and wireless communication
PPT
Http request&response by Vignesh 15 MAR 2014
PDF
EAI design patterns/best practices
PPT
10135 a 05
DOCX
CSE422 Section 002 – Computer Networking Fall 2018 Ho.docx
PPT
Http request&response session 1 - by Vignesh.N
PDF
Introduction to HTTP
PPTX
Difference between Client Polling vs Server Push vs Websocket vs Long Polling
PPTX
Basic understanding of websocket and and REST API
PPT
Under the Covers with the Web
PPTX
Application layer
PPTX
Web Real-time Communications
PPTX
HTTP Presentation(What exactly is http).pptx
PPTX
Unit-3_application layer .pptx
PPTX
Unit-3_application layer of osi model.pptx
PDF
Understanding the Web through HTTP
PPTX
DNS & HTTP overview
PPTX
Fight empire-html5
PDF
Computer networks module 5 content covered in this ppt
Taking a Quantum Leap with Html 5 WebSocket
Module 5.pptx HTTP protocol on optical and wireless communication
Http request&response by Vignesh 15 MAR 2014
EAI design patterns/best practices
10135 a 05
CSE422 Section 002 – Computer Networking Fall 2018 Ho.docx
Http request&response session 1 - by Vignesh.N
Introduction to HTTP
Difference between Client Polling vs Server Push vs Websocket vs Long Polling
Basic understanding of websocket and and REST API
Under the Covers with the Web
Application layer
Web Real-time Communications
HTTP Presentation(What exactly is http).pptx
Unit-3_application layer .pptx
Unit-3_application layer of osi model.pptx
Understanding the Web through HTTP
DNS & HTTP overview
Fight empire-html5
Computer networks module 5 content covered in this ppt

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
KodekX | Application Modernization Development
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPT
Teaching material agriculture food technology
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Cloud computing and distributed systems.
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Modernizing your data center with Dell and AMD
Unlocking AI with Model Context Protocol (MCP)
The AUB Centre for AI in Media Proposal.docx
KodekX | Application Modernization Development
Advanced methodologies resolving dimensionality complications for autism neur...
Teaching material agriculture food technology
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Encapsulation_ Review paper, used for researhc scholars
Understanding_Digital_Forensics_Presentation.pptx
NewMind AI Weekly Chronicles - August'25 Week I
Spectral efficient network and resource selection model in 5G networks
Reach Out and Touch Someone: Haptics and Empathic Computing
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Digital-Transformation-Roadmap-for-Companies.pptx
Cloud computing and distributed systems.
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Empathic Computing: Creating Shared Understanding
The Rise and Fall of 3GPP – Time for a Sabbatical?
Dropbox Q2 2025 Financial Results & Investor Presentation
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Modernizing your data center with Dell and AMD

Realtime Communication Techniques with PHP

  • 1. Realtime Communication Techniques with PHP Scott Mattocks & Chris Lewis, OnForce, Inc.
  • 2. Agenda Introductions Overview User expectations Problems with delivery Techniques Refresh Short Polling Long Polling WebSockets Q&A
  • 3. Introductions Scott Mattocks Senior PHP developer at OnForce, Inc. Contributor to PEAR Author of Pro PHP-GTK Chris Lewis Senior PHP developer at OnForce, Inc. 6 years of enterprise PHP experience 12 years of front-end web development experience Both are contributors to the WaterSpout Real-time Communication Server
  • 4. Overview Example: http://www.spoutserver.com Click “John Locke” in the Demos section
  • 5. Overview Users expect data to be delivered more quickly and seemlessly Gmail changed the way user expect to interact with websites Loading an entire page to show a small change is slow and wasteful
  • 6. Problems with delivery The web runs (for the most part) on HTTP HTTP is one way User asks for data Server sends data back The server can't do anything without the user asking first
  • 7. Problems with delivery Timeliness There is a gap between arrival of the data on the server and notification of the user. We want to reduce the gap without killing the servers. Client Server New Data New Data Black out
  • 8. Problems with delivery Efficiency How much of the transfered data is important to the user? Data transfer: X bytes sent for request (GET headers) Y bytes sent for response (body and headers) Z bytes of new data Z / (X + Y) = efficiency We want try to send only the data that has changed and that is important to the user
  • 9. Problems with delivery Scalability What kind of load are we causing on the server? Are we holding resources (i.e. database connections) that could be used to server other users? We want to do more with less
  • 10. Solutions Refresh the page Short Polling Long Polling 1 server 2 servers WebSockets
  • 11. Solutions @gnomeboy want to come over for some cookies? @gnomegirl hellz yeah! Example: Simple twitter feed style page
  • 12. Solutions Page consists of a wrapper around message content Avg message size = 100 bytes Avg page size (without images) = 4 KB Avg request headers = 410 bytes Avg response headers = 210 bytes (JavaScript code examples use Prototype)
  • 13. Refresh The same as if the user hit F5 Reloads the entire page New page load adds the new message No constant connection to the server Black out periods between requests
  • 14. Refresh database web server web server
  • 15. Refresh Difficulty 1 out of 10 Use HTML or JavaScript <meta http-equiv=&quot;refresh&quot; content=&quot;5&quot; /> setTimeout('window.location.reload(true)', 5000);
  • 16. Refresh Timeliness Timeliness of data depends on the refresh rate There is an additional delay for the round trip to the server Includes time to send request, process request, and send response
  • 17. Refresh Efficiency (assume 10 total requests, 1 new message) Data transfer: 410 bytes sent for each request 4210 bytes sent for each response, 4310 for new message response 100 bytes of new data 100 / ((410 + 4210) * 10 + 100) = .0021 per message Server resources used 1 HTTP process per request (10 total) 1 Database connection per request (10 total)
  • 18. Short Polling Ask the server for updates since the last time you asked Like a road trip with a 3 year old Are we there yet? Are we there yet? No constant connection to the server Black out periods between requests Cursor required to prevent data loss
  • 19. Short Polling database web server web server
  • 20. Short Polling Difficulty 5 out of 10 AJAX Background request to the server checks for new messages at a timed interval New messages are added to DOM by JavaScript
  • 21. Short Polling function poll_server () { new Ajax. Request ( this .urlPolling, { method: 'post' , parameters: data, onSuccess: this .successHandler, onFailure: this .handleFailure, onException: this .handleException }); } setInterval ( 'poll_server()' , 5000 );
  • 22. Short Polling function successHandler (trans, messages_div) { if (trans.responseText) { var json = trans.responseText. evalJSON (); if (json.message) { var msg = json.message + '<br />' ; $(messages_div). insert (msg); } } }
  • 23. Short Polling function successHandler (trans, messages_div) { if (trans.responseText) { var json = trans.responseText. evalJSON (); if (json.message) { var msg = json.message + '<br />' ; $(messages_div). insert (msg); } } }
  • 24. Short Polling Timeliness Timeliness of data depends on the refresh rate There is an additional delay for the round trip to the server Includes time to send request, process request, and send response
  • 25. Short Polling Efficiency (assume 10 total requests, 1 new message) Data transfer: 410 bytes sent for each request 210 bytes sent for each empty response, 310 for new message response 100 bytes of new data 100 / ((410 + 210) * 10 + 100) = .0158 per message Server resources used 1 HTTP process per request (10 total) 1 Database connection per request (10 total)
  • 26. Long Polling - 1 server Ask the server for updates since the last time you asked Server does not respond until a new message has arrived No constant connection to the server Black out periods between sending response and next request Cursor required to prevent data loss
  • 27. Long Polling – 1 server database web server web server
  • 28. Long Polling – 1 server Difficulty – Client side 5 out of 10 AJAX Background request to the server checks for new messages and waits for responses New messages are added to DOM by JavaScript
  • 29. Long Polling – 1 server Difficulty – Server side 5 out of 10 Continuously loop checking for new messages Break out of loop when new message arrives
  • 30. Long Polling – 1 server function poll_server () { new Ajax. Request ( this .urlPolling, { method: 'post' , parameters: data, onSuccess: this .successHandler, onFailure: this .handleFailure, onException: this .handleException }); } document. observe ( 'dom:loaded' , poll_server);
  • 31. Long Polling – 1 server function successHandler (trans, messages_div) { if (trans.responseText) { var json = trans.responseText. evalJSON (); if (json.message) { var msg = json.message + '<br />' ; $(messages_div). insert (msg); } } poll_server (); }
  • 32. Long Polling – 1 server <?php $query = 'SELECT message FROM messages WHERE dateadded > ?' ; $stmt = $pdo -> prepare ( $query ); while (true) { $message = $stmt -> execute ( $cursor_date ); if (! empty ( $message )) { echo json_encode ( $message ); break ; } } ?>
  • 33. Long Polling – 1 server Timeliness Near real-time As soon as a message shows up on the server, it is sent to the waiting client Black out period between sending response and making new request can add some delay
  • 34. Long Polling – 1 server Efficiency (1 request, 1 new message) Data transfer: 410 bytes sent for request 310 bytes for new message response 100 bytes of new data 100 / (410 + 310) = .138 per message Server resources used 1 HTTP process 1 Database connection
  • 35. Long Polling – 2 servers 2 servers work together to use fewer resources Main web server handles initial page request Main server informs polling server when new message arrives Polling server informs client No constant connection to server Black out periods between sending response and next request Cursor required to prevent data loss
  • 36. Long Polling – 2 servers web server polling server HTTP XHR over HTTP
  • 37. Long Polling – 2 servers Difficulty – Client side 5 out of 10 AJAX Background request to the server checks for new messages and waits for responses New messages are added to DOM by JavaScript
  • 38. Long Polling – 2 servers Difficulty – Server side 8 out of 10 Web server makes cURL call to polling server when a new message is added Polling server determines who message is for and sends the response to the waiting connection
  • 39. Long Polling – 2 servers function poll_server () { new Ajax. Request ( this .urlPolling, { method: 'post' , parameters: data, onSuccess: this .successHandler, onFailure: this .handleFailure, onException: this .handleException }); } document. observe ( 'dom:loaded' , poll_server);
  • 40. Long Polling – 2 servers function successHandler (trans, messages_div) { if (trans.responseText) { var json = trans.responseText. evalJSON (); if (json.message) { var msg = json.message + '<br />' ; $(messages_div). insert (msg); } } poll_server (); }
  • 41. Long Polling – 2 servers Main Web Server Polling Server <?php $query = 'INSERT INTO messages VALUES (?, ?)' ; $stmt = $pdo -> prepare ( $query ); $stmt -> prepare ( $message , $date ); $polling_server -> send ( $message ); ?> <?php foreach ( $this ->waiting as $connection ){ $connection -> respond ( $message ); } ?>
  • 42. Long Polling – 2 servers Timeliness Near real-time As soon as a message shows up on the server, it is sent to the waiting client Black out period between sending response and making new request can add some delay
  • 43. Long Polling – 2 servers Efficiency (1 request, 1 new message) Data transfer: 410 bytes sent for request 310 bytes for new message response 100 bytes of new data 100 / (410 + 310) = .138 per message Server resources used 1 HTTP process on polling server per message 0 Database connections
  • 44. WebSockets According to http://en.wikipedia.org/wiki/WebSocket: „ WebSockets is a technology providing for bi-directional, full-duplex communications channels, over a single Transmission Control Protocol (TCP) socket, designed to be implemented in web browsers and web servers”
  • 45. WebSockets WebSockets allow server to push data to client Connection established via client-initiated handshake After handshake, both the client and the server can send and recieve data Server can send data without the client asking for it first
  • 46. WebSockets Builds off of 2 server long polling Main web server communicates with event server Constant connection between client and server No black out periods No need for cursor
  • 47. WebSockets web server WebSocket server JSON via WebSockets
  • 48. WebSockets Difficulty – Client side 5 out of 10 WebSocket API Connection established onMessage events fired when new data comes in from the server New messages are added to DOM by JavaScript
  • 49. WebSockets Difficulty – Server side 8 out of 10 Web server makes cURL call to WebSocket server when a new message is added WebSocket server determines who message is for and sends the response to the waiting connection
  • 50. WebSockets function poll_server () { var socket = new WebSocket ( 'ws://example.com/' ); socket.onmessage = function (response) { successHandler (response, 'message_errors' ); } } document. observe ( 'dom:loaded' , poll_server);
  • 51. WebSockets function successHandler (trans, messages_div) { if (trans.data) { var json = trans.data. evalJSON (); if (json.message) { var msg = json.message + '<br />' ; $(messages_div). insert (msg); } } }
  • 52. WebSockets Main Web Server WebSocket Server <?php $query = 'INSERT INTO messages VALUES (?, ?)' ; $stmt = $pdo -> prepare ( $query ); $stmt -> prepare ( $message , $date ); $polling_server -> send ( $message ); ?> <?php foreach ( $this ->waiting as $connection ){ $connection -> respond ( $message ); } ?>
  • 53. WebSockets Timeliness Real-time As soon as a message shows up on the server, it is sent to the waiting client No black out period
  • 54. WebSockets Post Handshake Efficiency Data transfer: 0 bytes sent for request 100 bytes for new message response 100 bytes of new data 100 / 100 = 1.000 per message Server resources used 1 process on WebSocket server 0 Database connections
  • 55. WebSockets Efficiency Demo http://spoutserver.com/demos/compare Comparison Refresh: .0021 Short Polling: .0158 Long Polling: .138 WebSockets: 1.00
  • 56. WaterSpout Lightweight HTTP server Static files Dynamic content (PHP) Best used as part of event-driven server pair Can function as both ends Handles WebSockets and long polling Written in PHP
  • 57. WaterSpout Two types of connections Listeners Updates When an update comes in the appropriate listeners are notified Custom controllers define which listeners should be notified
  • 58. WaterSpout - Listeners <?php public function listen () { // Handle cursor for long polling fall back. // ... $this ->dispatcher-> add_listener ( $this ); } ?>
  • 59. WaterSpout - Dispatcher <?php /* Called every .25 seconds on all waiting listeners */ public function process_event (Controller $mover = null) { $key = array_search (( int ) $this ->_cursor, array_keys (self:: $_commands )); if ( $key === false && ! is_null ( $this ->_cursor)) { return ; } $commands = array_slice (self:: $_commands , $key ); if ( empty ( $commands )) { return ; } $response = new HTTPResponse ( 200 ); $body = array ( '__URI__' => $this ->uri, 'commands' => $commands, 'cursor' => end ( array_keys (self:: $_commands )) + 1 ); $response -> set_body ( $body , true); $this -> write ( $response ); $this ->_cursor = ( int ) end ( array_keys (self:: $_commands )) + 1 ; } ?>
  • 60. WaterSpout vs Apache/Nginx Neither Apache nor Nginx has a way for one process to notify the listeners You need to put the incoming update into some shared location (mysql, memcache, etc) Listeners have to poll the shared location Heavy Slows down timeliness WaterSpout solves this by letting updates notify listeners
  • 61. WebSockets Resources http://dev.w3.org/html5/websockets/ http://en.wikipedia.org/wiki/Web_Sockets http://www.spoutserver.com/
  • 62. Questions? Contact [email_address] Twitter @spoutserver Feedback http://joind.in/talk/view/1748 Slides http://www.slideshare.net/spoutserver

Editor's Notes

  • #6: - Once upon a time, the internet was flat. Users asked for content and web servers gave it to them. - XMLHttpRequest came along and change all of that. - Google said “well, we know they are going to ask for their new messages soon anyway, so why not give them the new messages before they ask?”
  • #10: - Unless you are Facebook, the database is your bottle neck. - Solutions that consume database resources are not very scalable - Timeliness is often sacrificed for scalability
  • #20: Basically the same as refresh but with less data transfered