Tuesday, April 08, 2008

Frankie Goes To Facebook

UPDATED 4/12/2008: Now compatible with Sinatra 0.2.0
So you want to create a small Facebook application... seems like it should be a small thing, right? But creating an entire Ruby on Rails application just for a tiny little Facebook application is, at the very least, a bit wasteful. In the case of a overly popular Facebook app you could end up with, as Marc Andreessen put it, a "self-inflicted denial of service attack", unless you have both a pretty serious infrastructure to support it, as well as lots of cash to keep that data center running.

Wouldn't you rather be able to create a highly scalable "hello, world" Facebook application in around 13 lines of Ruby code? Say hello to Frankie:

require 'rubygems'
require 'frankie'

configure do
set_option :sessions, true
load_facebook_config "./config/facebooker.yml", Sinatra.env
end

## facebooker helpers
before do
ensure_authenticated_to_facebook
ensure_application_is_installed_by_facebook_user
end

## the site
get '/' do
body "<h1>Hello #{session['facebook_session'].user.name} and welcome to frankie!</h1>"
end


Frankie is a plugin for the minimalist and very fast Sinatra web framework that allows you to easily create a Facebook application by using the Facebooker gem. Why would you want to use it? If your Facebook application needs to be highly scalable, is fairly small, or is really a mashup of other web-available resources, than Frankie could be a good solution.

Frankie is available now for your enjoyment. Here is how to get started:

- Install the Frankie gem, which will install the Sinatra and Facebooker gems if you do not already have them.
sudo gem install frankie


- Create the application directories for your new app:
mkdir myapp
cd myapp
mkdir config


- Put your facebooker.yml file into the /myapp/config directory, and set the values to your information. Here is a simple example of the file:

-------
development:
api_key: apikeyhere
secret_key: secretkeyhere
canvas_page_name: yourcanvashere
callback_url: http://localhost:4567
test:
api_key: apikeyhere
secret_key: secretkeyhere
canvas_page_name: yourcanvashere
callback_url: http://localhost:4567
production:
api_key: apikeyhere
secret_key: secretkeyhere
canvas_page_name: yourcanvashere
callback_url: http://yourrealserver.com


- Make sure you have setup your facebook application on the facebook site. Google "setup new facebook application" if you are unsure how to do this. I recommend starting with an IFrame application, so that you can point a development version of your Facebook application to your local machine. This, like our example here, would use "http://localhost:4567" as the callback URL when configuring the Facebook app.

- Create your application, based on the sample above, and run it:
ruby sample.rb


This will start the Sinatra Ruby web server running:
== Sinatra has taken the stage on port 4567!


- Test it by pointing your browser to http://apps.facebook.com/yourappname. If you have things setup correctly then you should see your application appear inside of Facebook's site.

Facebook is now your playground... have fun with Frankie!

11 comments:

bmizerany said...

This is killer! I've been waiting for a facebook-sinatra gem since launch and this is it!

bmizerany said...

here is a tunnel script I stole and modified http://pastie.caboo.se/178903 for easy local development of facebook apps

Ron Evans said...

Thanks, Blake! I appreciate your fine work on Sinatra. Also, thanks for the tunnel script. I think the original link you meant was to Evan Weaver's blog.

bmizerany said...

heh. It was actually another blog that adapted theirs from Evan Weavers. ;) This tunnel script is framework agnostic.

josh said...

I'm getting a wacky error:

Facebooker::Session::IncorrectSignature

I'm using the latest gem installs of everything.

Any ideas?

Ron Evans said...

Hi, Josh!

Usually you would get the Facebooker::Session::IncorrectSignature error if you try to navigate directly to the server with your facebook application like http://localhost:4567 instead of navigating to http://apps.facebook.com/myappname and letting Facebook load the app from your server.

Hope that helps!

kikkamokkeli said...

I got a NoMethodError - undefined method `user' for nil:NilClass on this line:

body "<h1>Hello #{session['facebook_session'].user.name} and welcome to frankie!</h1>"

I replaced it with the line below and got it working.

body "<h1>Hello #{session[:facebook_session].user.name} and welcome to frankie!</h1>"

mj said...

Just got Frankie running with Sinatra 0.9.3 had to make some edits, the are noted on this ticket: http://sinatra.lighthouseapp.com/projects/9779-sinatra/tickets/130-add-deprecated-sinatraeventcontext-to-unbreak-plugins#ticket-130-3

Thomas010 said...

I'm sorry... I'm just someone looking to make a very minimalist, non-invasive survey for Facebook. I have never made an application for Facebook before, and I would like to use your apparently very simple Frankie (gem?) and just get my survey on Facebook as quickly as possible. Do you have any advice for going from complete ignorance to the beginning of your post?

Chuck said...

Ron, I work with a travel portal called JohnnyJet.com

We would love to talk about developing a Facebook Application. If your're interested contact me at chuck@johnnyjet.com

Tks,

Chuck said...

Ron,

I'm with a travel portal called JohnnyJet.com We are starting the development process to auhtor a Facebook application. We'd love to talk to you if you would like to be apart of the proogram. I can be reached at:

chuck@johnnyjet.com