Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Meteor and The Real Time Web

Meteor and The Real Time Web

I gave a presentation to PHP London about my open source project, Meteor, which is a new kind of HTTP server designed to stream text data to browsers in real time, enabling applications like chat, news updates, tickers, stick price changes etc.

Andrew Betts

April 20, 2012
Tweet

More Decks by Andrew Betts

Other Decks in Technology

Transcript

  1. Open  source  project  developing  a   streaming  data  server  over

     HTTP   UK’s  first  property  search  engine  map,  and   the  first  British  Google  Maps  mashup  (?)   Day  job.   Hats:  
  2. •  Why  we  need  a  real  time  web   • 

    When  to  consider  integrating   real  time  data   •  Demo  of  Meteor   •  Integrating  with  PHP   •  Q&A   Agenda:   Characters reproduced (badly) from some very nifty BUPA adverts.
  3. Typically  information  on   the  web  is  requested  by  

    the  client   Sometimes,  this  model   is  very  inefficient...  
  4. Problem:  the  client   doesn’t  know  when  to  ask  

    So  it  keeps  asking  again   and  again...  
  5. Other  problems  with   short  polling   •  Repetitive  database

     queries   •  Unnecessary  load   •  Bandwidth  use   •  Negative  responses   •  Cookies!   •  Delay  before  client  sees  data   •  Unpredictable  data  delivery  times   and  all  this  because…  
  6. So  where  does  it  come  from?   •  Other  clients/users

      •  External  events   •  Status  changes   •  Progress  feedback  
  7. Users  interacting  with  other  users   see  the  site  as

     a  representation  of   all  those  other  people.   So,  they  need  to  know  when   their  context  changes.  
  8. Meteor  keeps  each  user’s  context   fresh  with  new  information

     delivered   as  it  becomes  relevant  to  that  user.  
  9. Data  is  sent  when  it’s  ready  on  a   persistent,

     preestablished     connection,  enabling:   Streaming   It’s  HTTP,  Jim,  but  not  as  we  know  it*   *Apologies  for  Star  Trek  reference  
  10. •  Create  messages   •  Inject  messages  into  Meteor  on

      a  channel   •  Can  read  stats  back  from   Meteor   Event  controllers  
  11. Sample  event  controller  in  PHP   // Create an array

    of words $sayings = array("the", "quick", "brown", "fox", "jumps", "over", "lazy", "dogs"); // Open a controller channel to Meteor server $op = fsockopen("127.0.0.1", 4671, $errno, $errstr, 5); // Write a random word $out = "ADDMESSAGE demo ".$sayings[array_rand($sayings)]."\n"; fwrite($op, $out); // Read response $buf = fread($op, 4096);
  12. •  Listen  on  one  or  more  channels   •  Meteor

     Javascript  library   handles  comms   •  Hook  in  your  own  callback   functions   Subscriber  clients  
  13. Sample  subscriber  client  JS   <script type="text/javascript" src="http://data.example.com/ meteor.js"></script> <script

    type="text/javascript"> var mymeteor = new Meteor(); mymeteor.subdomain = “data"; mymeteor.registerEventCallback("process", newMsg); mymeteor.joinChannel("demo", 5); mymeteor.start(); function newMsg(strmsg) { alert("Meteor said " + strmsg); } </script>
  14. •  Apache:  example.com   •  Meteor:  data.example.com     • 

    Different  hostnames   •  Can  be  same  machine  via  port   redirection  or  IP-­‐binding   Separating  servers  
  15. •  A  Publish-­‐Subscribe  system   •  A  great  way  of

     broadcasting   updates  to  groups  of  subscribers   So  in  summary,  Meteor  is:   •  A  way  of  pushing  data  to   individually  addressable  clients   Meteor  is  not:  
  16. Who  is  the  user  interacting  with?   Geoff    

    Stockbroker,  age  49   Wants  financial  news,  and  no  mucking  about   Deirdre     Retired  teacher,  age  72   Does  her  banking  online  and  books  trips  abroad   Amy     Student,  age  23   Facebook  is  her  life   NO-­‐ONE   SITE   OTHER   USERS