Speeding up Tunnlr

Posted: September 8th, 2009 | Author: | Filed under: Facebook, Rails | 2 Comments »

When developing a Facebook or Facebook Connect application, there are times when Facebook servers will need to directly make requests to your application. This means your local web server has to be remotely accessible for incoming requests.

Typically, when working locally, your mongrel or webrick instance running on port 3000 is not accessible unless you enable port forwarding on your router. And even then, your ISP probably doesn’t assign you a fixed IP address which means you have to run a dynamic IP address service like dyndns. This quickly gets complicated and if you work behind a corporate network or develop from different locations (ex.: home, office, Starbucks…), this solution won’t even work.

Meet Tunnlr, an easy to use service that makes your local Rails server accessible from the Internet by just typing a single rake command in your console. When you register to the service, you will be assigned a fixed URL address with a port number and this is the address you will use to configure your Facebook application and access it locally.

One downside to that approach is that since each request has to do a return trip to the Tunnlr servers, you loose the lightning speed loading time you’re used to when developing from your local machine (no network latency). I found a little hack that will bypass the need to do a return trip and it can be done in three easy steps

1- Modify your /etc/hosts file and add the following line :

127.0.0.1	web1.tunnlr.com

This tells your machine to route all requests made to the web1.tunnlr.com address to your local machine. Unfortunately, there is no way to specify port redirections in the hosts file so we need step 2 and 3 to make sure our remote and local ports are the same.

2- Modify your tunnlr.yml or facebooker.yml file to make your local port the same as your remote port. In my case, this is port 10524.

remote_port: 10524
local_port: 10524

3- Start your Rails server with the same port that has been assigned to you by Tunnlr.
[/code]script/server -p 10524[/code]

That’s it! Now when you query web1.tunnlr.com your local requests will stay local and your server will still be remotely accessible from the tubes!


redirect_to :back

Posted: November 5th, 2008 | Author: | Filed under: Rails | 1 Comment »

Here’s a useful trick I didn’t know about! The command redirect_to :back does just what it says, it redirects the user back to the previous action. This is similar to redirect_to(request.env["HTTP_REFERER"]).

The Rails documentation states that this can be useful for forms that are triggered from multiple places.

In my case I needed it to redisplay the same page after a post that didn’t validate. I could redirect the user back to the right action but some request parameters were missing in the post action that prevented this to work right.

For example, if the last request was /my_action?location=there, I would lose the location parameter if I didn’t send it along in the post. And I didn’t want to send the parameter along since I was calling a different controller which business logic didn’t relate to these parameters.

Well, by using the HTTP referer, Rails redirects back to the URL with the right parameters and sends the user to the exact same location he was. Neat!


Alternate background colors with jQuery

Posted: September 26th, 2008 | Author: | Filed under: Javascript, jQuery | 1 Comment »

I did a little javascript function with jQuery this morning to alternate DOM elements background colors. The function takes in parameters an array of jQuery elements and an array of hexadecimal colors and changes the background color of each element with the next color in the array.

// Alternates the background colors of the elements
// elems  - Array of jQuery DOM objects
// colors - Array of hexadecimal colors
function alternateBgColor(elems, colors) {
    elems.each( function(index) {
    var colorIndex = index % colors.length;
    $(this).css('background-color', colors[colorIndex]);
  });
}

It’s only a three lines function but the cool thing about it is that you can alternate through as many colors as you need. I’m only using two for now but this could come useful at some point! I like how everything javascript just got easier with jQuery.