How to make Flex and Rails maintain your Facebooker Session
This might possibly be a firefox specific issue. If you’ve ever been building a Flex-based Facebook application and are using Ruby on Rails on the backend, you may have come across this problem.
When your Facebook application first loads your SWF from the Rails server, its actually being proxied through Facebook. Which means the first request has all your vital fb_sig data, as well as your facebooker session object through which all your API calls are going to be made.
The fun part is that any subsequent Flex -> Rails calls that are made asynchronously are going to get a brand new rails session, for some reason. And that session is not going to have any of the facebook specific connection information, which means its basically like you’re not on facebook at all.
So the way to fix this is to just go ahead and pass your originally created session ID into the FlashVars that get loaded along with the SWF on the first page load. In our main controller’s action that is loading the SWF we already have it setup to pass some flash vars along, so I just added one more:
@flash_vars_str += “session_id=#{session.session_id}&”
Then don’t forget to pass those vars along to your fb:swf tag:
<fb:swf
swfsrc=”<%= Facebooker.facebooker_config['callback_url'] %>/swf/Main.swf”
flashvars=”<%= @flash_vars_str %>”
height=”860″
width=”755″ />
<script>
Now in your flex app, whenever you do any kind of call back to your Rails server, make sure to tack that session ID onto the request parameters. The default way to access it would be something like
this.parentApplication.parameters["session_id"]
But your Flex environment problably has accessors to the flash vars differently.
One last note, is that if you want that session ID to actually be recognized on the Rails side and used instead of creating a new session, you need to attach it to your request as a named parameter called “_session_id”. Note the leading underscore.
Of course if you’re like us, your session key is actually renamed to be “_{RAILS_APP}_SESSION_ID”, which means you need to pass that along to your flex app in the flash vars as well, otherwise it won’t get recognized.
There’s probably a reason why the sessions aren’t getting maintained by default, but hell if I know what it is.
