Hope you can help, I'm kind of at my wit's end here....I'm working on a Rails App and running Koala to connect with the Facebook Graph API. I want to maintain an up-to-date list of a user's friends who are using my app so I have been trying to implement the Realtime Updates functionality of the Koala gem. My app is deployed on Heroku and I'm running Resque with Redis-to-Go to manage calls from Facebook... unfortunately, I don't seem to be receiving any even though my test pings to the Facebook server are all returning true. Here's the code I'm working with:
Realtime Controller:
class RealtimeController < ApplicationController
skip_before_filter :logged_in_check
layout nil
VERIFY_TOKEN = "purple-rain"
def create
Friendship.real_time_update!(params)
render :text => 'success'
end
def index
Rails.logger.info("RealTimeController verification")
render :text=>Koala::Facebook::RealtimeUpdates.meet_challenge(params, VERIFY_TOKEN)
end
end
Friendship Controller:
class Friendship < ActiveRecord::Base
attr_accessible :friend_id, :user_id
belongs_to :users, :foreign_key => "user_id"
belongs_to :friends_ids, :class_name => "users", :foreign_key => "friend_id"
def self.get_friends(user)
@facebook ||= Koala::Facebook::API.new(user.oauth_token)
@friends = @facebook.get_connection(user.fbid, "friends")
@friends.each do |friend|
if @friend = User.find_by_fbid(friend["id"])
friendship = Friendship.find_or_create_by_user_id_and_friend_id!(user.id,
@friend.id)
end
end
end
def self.real_time_update!(payload)
RealtimeUpdate.new(payload).enqueue_updates!
end
class RealtimeUpdate < Struct.new(:payload)
def enqueue_updates!
remove_duplicate_ids.each do |entry|
if (user = User.find_by_uid(entry['uid']).try(:user))
Resque.enqueue(Resque::Job::UpdateFacebookFriends, user.id,
:since=>entry['time'])
end
end
end
protected
def remove_duplicate_ids
payload['entry'].each_with_object({}) do |entry, hash|
hash[entry['uid']] ||= [] << entry
end.values.collect { |update_payloads| update_payloads.min { |entry1, entry2|
entry1['time']<=>entry2['time'] } }
end
end
end
Resque Job
class Resque::Job::UpdateFacebookFriends
@queue = "facebook_friends"
def self.perform(uid, opts={})
::Timeout.timeout(1800) do
Friendship.get_friends!(User.find_by_fbid(uid))
end
end
end
Happy to provide any other info I can!!!
Thanks in advance!