Rack and microweb frameworks

From DevSummit
Jump to: navigation, search

Facilitated by Evan Henshaw-Plath, protest.net

Session Description

Ruby on Rails, Django, and their ilk are the new bloated elephants.Today developers are searching for something even faster and lighter. Taking advantage of HTTP, sockets, and the magic of UNIX, there are new light and fast web frameworks. They're built on Rack, a json http protocol to do layered processing on the server. It's the ease of php with the maintainability of a non-crazy language.

Session Notes

Josh's notes: I'll confess that some of this material went whistling over my head. I humbly request those with clearer understanding to correct all errors. Thanks!

History: About five years ago there was a movement from Java/PHP apps to web frameworks like Rails, Django, CodeIgnitor, Symphony - these were faster, lighter implementations, using a "Model-View-Controller" architecture. Since then, that's become the establishment approach to web apps. Meanwhile, Python developers came up with WSGI (renamed as 'Rack' by the Ruby community) as a "micro framework." There are now a number of different micro frameworks (for example Sinatra, which also works with Ruby.)

What is it? At the server level, the application reparses the parameters in a URL as JSON-like filters, potentially sending activities off to different servers. These activities can be run in series. For example, there can be a separate authentication layer, which could be redirected if needed. Another example could be token checking, or serving up images from a separate image server. There can even be multiple requests that are handled at the same time - think of the different parts of a FaceBook page, which update independently while you're perusing the page.

Another way of thinking about it: in a traditional PHP script, the "print" statements send everything back to the browser, building up the page in the browser in successive chunks. Drupal sends everything to a template, which is served up. AJAX does all the processing itself. Micro Frameworks rethink that approach - it responds to the web request in many small, loosely joined pieces.

What's it good for? Applications using micro frameworks can be more maintainable, more manageable and more scalable. It's also very useful when you're working with multiple frameworks at the same time. Performance can also be greatly improved. It's all about getting content back to the user ASAP! Of course, if you have static content, you're best off with straight HTML, which is amazingly fast. (The ideal is to do as little processing as possible at the moment of the request.)

WebSockets: Some discussion of the WebSockets technology. This is implemented in most modern browsers. Allows the application to open a persistent connection, so that the server can "push" content to the browser - unlike traditional HTTP, where the server fulfills the browser's request and then the connection is closed.

How does it work in practice? Think of loading a page on the new FaceBook. When you make a request, the server sends a little HTML and a little JavaScript. The JavaScript opens a WebSocket back to the server, or (if JavaScript turned off) uses Flash to open the socket. (This doesn't have the cross-domain scripting problems you'd find in AJAX.) Meanwhile, each section of the page might be sent to a different server for response. These responses come back (perhaps as JSON) in any order to the browser's socket handler, where each is placed into the right part of the page.

Security: Rack lets you do data input filtering, so that user input can be scrubbed even before it hits the application.

Communities: There are lots of active development communities for frameworks & micro frameworks. Huge support for Ruby on Rails - your solution may already be out there, or there may be a plug-in to do what you want. Django doesn't have as much of a community.

Getting started: Sinatra is more complicated that PHP, but not too bad. A good way to get started with Sinatra or Rails is the site Heroku (which offers "Platform as a Service"). You can check in your code and they set it all up so you can test it - running it on a single processor is free; there's a cost when you get serious traffic.