11
From Web sites to Web apps in Rails Ambert Ho *all images were has7ly ripped off the internet and belong to their respec7ve owner

Realtime web apps rails

Embed Size (px)

Citation preview

Page 1: Realtime web apps rails

From  Web  sites  to  Web  apps    in  Rails  

Ambert  Ho  

*all  images  were  has7ly  ripped  off  the  internet  and  belong  to  their  respec7ve  owner  

Page 2: Realtime web apps rails

What  is  a  web  site  vs.  web  app?  

•   Content  consump7on  vs.  interac7on?  •   Facebook  and  TwiCer  blur  the  line  

Page 3: Realtime web apps rails

Emerging  importance  of  ‘real7me’  

•   Applica7ons  should  change  state  regardless  of  interac7on  •   Even  for  content  sites:  •   TwiCer  and  Google  News  update  themselves  while  you  look  at  it  •   Same  with  Facebook  wall,  plus  FB  no7fica7ons  

•   Previously,  ‘real7me’  primarily  used  for  chat  

I  use  this  term  loosely  since  ‘real7me’  has  predefined  meanings  in  terms  of  RTOS  

Page 4: Realtime web apps rails

How  does  it  work?  

Page 5: Realtime web apps rails

What  is  comet/reverse  ajax/ajax  push?  (‘push’  data  to  client)  

•   Long  polling  

•   Websockets  

•   (flash  sockets  too)  

Page 6: Realtime web apps rails

Details:  how  to  actually  go  about  doing  this?  

•   To  bolt  onto  Rails  you  need:  1.  A  real7me  endpoint  (more  on  this  later)    2.  Way  to  communicate  with  that  endpoint  3.  Clientside  code  organiza7on  to  process  what  happens    

“Gimme  a  real  example”  

Page 7: Realtime web apps rails

Rails  

Juggernaut  •   hCps://github.com/maccman/juggernaut  

•   Used  to  be  EM,  now  uses  Node.js  

Node.JS  Gem  for  interface  Redis  

Client  

Socket.IO  (client)   Your  JS  

Socket.IO  (server)  

Juggernaut  logic  

Page 8: Realtime web apps rails

Event  Loop  (For  the  real7me  endpoint)    

•   Why  not  use  Rails  all  the  way?  

•   Your  web  server  spins  up  a  worker  pool  of  en7re  Rails  instances  

•   #  persistent  connec7ons  limited  by  #  Rails  instances  that  fit  in  memory  

•   spinning  up  Rails  also  takes  7me  •   BeCer  with  REE  and  CoW  

•   Event  loops  excel  at  I/O  bound  tasks*  

•   Lot  of  simple  web  stuff,  par7cularly  managing  connec7ons  like  with  ‘real7me’,  is  I/O  bound  

*It’s  more  complicated  If  you  want  to  know  why  research  concurrency  on  JVM  

Page 9: Realtime web apps rails

Review  on  ‘what  does  what’  Rails,  using  the  Redis  gem  or  some  wrapper  like  the  Juggernaut  gem,  publishes  to  Redis,  and  since  Node.js  is  subscribed  it  gets  the  message,  which  it  boots  to  the  client  through  Socket.IO  –  the  client  then  uses  a  framework  (like  Backbone.js)  to  work  with  the  piece  of  data.  

•   Backbone  is  designed  towards  working  with  serialized  remote  data  

Do  it  OOB?  (Net::HTTP  is  blocking)  

Page 10: Realtime web apps rails

Alterna7ves  •   Pusher  •   Don’t  have  to  setup  Node  and  Redis  

•   Considera7on  with  websockets  (HTTP  1.1)  and  servers  proxied  behind  NGINX  

•   benefits  of  having  it  run  as  a  service,  but  costs  $$$  

•   Run  the  en7re  app  in  Node.js  •   LinkedIn  mobile  is  like  this  

•   Consider  hos7ng  (Dotcloud,  Heroku,  NodeJitsu)  

Page 11: Realtime web apps rails

Slides http://www.slideshare.net/ambertch

Contact [email protected]

Discussion/Questions?