realtime web apps rails
TRANSCRIPT
From Web sites to Web apps in Rails
Ambert Ho
*all images were has7ly ripped off the internet and belong to their respec7ve owner
What is a web site vs. web app?
• Content consump7on vs. interac7on? • Facebook and TwiCer blur the line
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
How does it work?
What is comet/reverse ajax/ajax push? (‘push’ data to client)
• Long polling
• Websockets
• (flash sockets too)
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”
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
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
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)
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)