Using SSH Tunnels to develop Facebook Applications


One of the most frustrating things I found early on when working on facebook applications, was that there was no simple way to create a local development environment. Because so many of the features of the platform existed only when the code was executed THROUGH the facebook proxy (fbml parsing, fb:ref cache content, etc), I found myself in an almost endless cycle of “svn commit”, “svn up” on the test server. Although I could have reduced this to a single step using a capistrano deployment task, or (heavens forbid) simply edited the code in place on the server using VI, I wasn’t happy with either of these options.

By happy coincidence, I spent the day hacking away on FB apps with my friend Ben Savage, who showed me how *he* does it – he simply port forwards port 4100 on his local router into his laptop, and then sets the facebook application to use his current outside IP:4100 as the app url.

I didn’t like this for a few reasons – first, it requires admin access to the router, a no-go from Starbucks. Second, I don’t like having to change my facebook application settings every time I change offices. So I decided to try using an ssh tunnel.

After mucking about a bit, I got it working! Here’s what I had to do (and yes, it’s pretty ugly and you probably want to do this on a non-critical junk proxy box you’ve got somewhere):

Changes to /etc/ssh/sshd_config

1. You have to make the ssh connection as root in order to bind to a privileged port (it’s probably trivial to use a non-privileged port for this, but I couldn’t be bothered). So you need to enabled root logins, by adding/changing
PermitRootLogin yes
2. In order to bind to a network address other than localhost on the remote server, you need
GatewayPorts yes

Then you simply run this from console on your local machine:

ssh root@mytunnelbox.com -p22 -N -R *:8090/localhost/80

This will forward port 8090 on your remote server to port 80 on your local workstation, via ssh tunnel. I don’t usually bother to fork into the background, because it’s nice to be able to ctrl-c to finish up.

Now get into your facebook developer application (you probably want to create a test application for this), and change the Callback URL to mytunnelbox.com:8090/. (You’ll only have to do this the first time).

Voila! Local development facebook application work – from Starbucks or anywhere else!

, , , , , ,

  1. #1 by admin on 14Feb08 - 1:17 pm

    Philipp Wassibauer over at Maptales has expanded on this with instructions for how to do this on a Windows machine, using Putty.

    Check it out:

    http://maptalesteam.blogspot.com/2008/02/ssh-tunnel-for-local-facebook-and.html

  2. #2 by Erez Segal on 30Apr08 - 9:21 am

    Amazing! This should be on the main page of the developers wiki!

  3. #3 by Robin Bolton on 20Oct08 - 5:43 pm

    Excellent tip. Even with my current single command deployment process (Ant build file which calls rsync), your method will save loads of time during development.

  4. #4 by Guy on 30Oct08 - 4:44 am

    Cool Blog!

    I was doing it already though, without encryption… I don’t see why this is useful.

    My problem was how to work from work ;-)

    Except from that, editing the files locally is something you can’t compete with, although I’m using mercurial as a source code repository, this way I’m tracking changes and are able to remotely work on the project.

  5. #5 by shaneel on 26Jan09 - 12:31 pm

    Thankyou very much , finally i have a hope to start with.
    Well done.
    Please be prepared for more questions ;)

  6. #6 by dbt on 01Jun09 - 3:02 am

    You're not binding to a privileged port, that's only stuff under 1024. So you don't need to log in as root to bind to 8090 at all. Try it!

  7. #7 by abhishek on 24Jan10 - 10:38 pm

    Hi Joshua,

    I was also trying my hand at heroku. (my first try at heroku, RoR and facebook app collectively)

    A simple test program using Facebooker app is running quite nice on my dev env. And I have ported it to http://evntize.heroku.com/evntize
    But now when I go to http://apps.facebook.com/evntize I see a blank page there. My canvas url is http://evntize.heroku.com/evntize
    Facebooker.yml has public port as 80 local port as 3000 (not sure how can I check my local port though) have used my mail id (abhishek.mancoha@gmail.com) as my public_host_user_name

    I am almost 100% sure that the problem is with the tunnel setup.
    My first question is do I need to be running heroku rake facebooker:tunnel:background-start on heroku? Is it even possible because on lots of blog post I have seen that heroku doesn’t support ssh and root access. Then how to go about it?

    Do you have facebook app demo/video on heroku?

    Also I have included Facebooker in tmp\vendor\plugins of my app. Haven’t been able to install rails plugin Facebooker on heroku was getting the wrong version of marshal file on trying to install that?

    Would be very thankful if you can help me out. Really want to go as fast as possible.

  8. #8 by Oliver on 16Mar10 - 7:49 am

    Really simple and works great.
    Thanks a lot.
    That will make facebook development much easier.

(will not be published)

Close
E-mail It