NeoSocial: Connecting to Facebook with Neo4j
Join the DZone community and get the full member experience.
Join For Free
social applications and graph databases go together like peanut
butter and jelly. i’m going to walk you through the steps of building an
application that connects to facebook, pulls your friends and likes
data and visualizes it. i plan on making a video of me coding it one
line at a time, but for now let’s just focus on the main elements.
the application will have two major components:
- a web service that handles authentication and displaying of friends, likes, and so-on.
- a background job service that imports data from facebook.
we will be deploying this application on heroku and making use of the redistogo and neo4j add-ons.
let’s start by cloning the application and creating it on heroku.
git clone git@github.com:maxdemarzi/neosocial.git heroku apps:create heroku addons:add neo4j heroku addons:add redistogo
since we are connecting to facebook, you will need to get a facebook app id and secret at https://developers.facebook.com/apps .
turn on “website with facebook login” and set it to your http://xxxxxxx.herokuapp.com domain.
come up with a session secret (any long text or sentence will do) and add it and your facebook parameters to your application.
heroku config:add session_secret=<your session secret> heroku config:add facebook_app_id=<your facebook app id> heroku config:add facebook_secret=<your facebook secret>
we now just need to deploy our application to heroku with a git push, and scale the number of workers to 1.
git push heroku master heroku ps:scale worker=1
if you go to your xxxxx.herokuapp.com domain, you should now see:
so what happens when the user clicks on “sign in with facebook”? they are sent to facebook to authenticate via oauth, and assuming they approve, a user object is created and they are sent to their profile page.
['get', 'post'].each do |method| send(method, "/auth/:provider/callback") do user = user.create_with_omniauth(env['omniauth.auth']) session[:uid] = user.uid redirect to(session[:redirect_url] || "/user/#{session[:uid]}") session[:redirect_url] = nil end end
let’s take a look at the create_with_omniauth method. it is creating a unique node using the facebook id, token, and values we received from authentication and returning a new user.
def self.create_with_omniauth(auth) values = {"name" => auth.info.name, "image_url" => auth.info.image, "location" => auth.info.location, "uid" => auth.uid, "token" => auth.credentials.token} node = $neo_server.create_unique_node("user_index", "uid", auth.uid, values) sidekiq::client.enqueue(job::importfacebookprofile, auth.uid) user.new(node) end
a node is just a hash, and we could build this whole app using plain hashes, but it makes life easier to build real objects and use them instead. here is our user class:
class user attr_reader :neo_id attr_accessor :uid, :name, :image_url, :location, :token def initialize(node) @neo_id = node["self"].split('/').last.to_i @uid = node["data"]["uid"] @name = node["data"]["name"] @image_url = node["data"]["img_url"] @location = node["data"]["location"] @token = node["data"]["token"] end ... end
using real objects allows us to tie in some methods to help us. for example the facebook client of this user, which uses the token we saved when authenticating and the koala gem to give us an approved connection to facebook.
def client @client ||= koala::facebook::api.new(self.token) end
let’s take one step back and look at the line before. it is using the sidekiq gem to kick off a background job named importfacebookprofile.
module job class importfacebookprofile include sidekiq::worker def perform(uid) user = user.find_by_uid(uid) ... # import friends friends = user.client.get_connections("me", "friends") friends.each do |friend| sidekiq::client.enqueue(job::importfriends, uid, friend["id"]) job::importmutualfriends.perform_at(120, uid, friend["id"]) end end end end
this worker is getting facebook friends of a user and then creating two sets of jobs. importfriends which gets added to the queue right away which does the actual importing of a friend, and importmutualfriends which gets added to the queue 2 minutes later.
module job class importfriends include sidekiq::worker def perform(uid, person_id) user = user.find_by_uid(uid) person = user.client.get_object(person_id) friend = user.create_from_facebook(person) # make them friends commands = [] commands << [:create_unique_relationship, "friends_index", "ids", "#{uid}-#{person_id}", "friends", user.neo_id, friend.neo_id] commands << [:create_unique_relationship, "friends_index", "ids", "#{person_id}-#{uid}", "friends", friend.neo_id, user.neo_id] batch_result = $neo_server.batch *commands ...
the importfriends job pulls the full friend profile from facebook and creates two “friends” relationships with the user (each going one way). the importmutualfriends job (shown in its entirety below) connects a friend to the other friends via the mutualfriends open graph api command:
module job class importmutualfriends include sidekiq::worker def perform(uid, person_id) user = user.find_by_uid(uid) person = user.client.get_object(person_id) friend = user.create_from_facebook(person) # import mutual friends mutual_friends = user.client.get_connections("me", "mutualfriends/#{person_id}") commands = [] # make them friends mutual_friends.each do |mutual_friend| uid = mutual_friend["id"] node = user.find_by_uid(uid) unless node person = user.client.get_object(uid) node = user.create_from_facebook(person) end commands << [:create_unique_relationship, "friends_index", "ids", "#{uid}-#{person_id}", "friends", node.neo_id, friend.neo_id] commands << [:create_unique_relationship, "friends_index", "ids", "#{person_id}-#{uid}", "friends", friend.neo_id, node.neo_id] end batch_result = $neo_server.batch *commands end end end
with these friends and friends of friends relationships we can use cypher inside our user model to gather a friend_matrix. note the use of parameterized cypher queries. it’s tempting to just insert it in to the string, but inefficient since neo4j would have to parse it every time. with parameters neo4j just parses it once and the next time it is ready to execute.
def friend_matrix cypher = "start me = node({id}) match me -[:friends]-> friends -[:friends]-> fof where fof <> me return friends.name, collect(fof.name) order by count(fof) " $neo_server.execute_query(cypher, {:id => @neo_id})["data"] end
some folks have hundreds of facebook friends, and our visualization doesn’t look too good once we get past 50 friends. so instead of visualizing all of the connections, we’ll take a random sample of 20-50 friends. we are simulating friends who randomly showed up to your birthday party. we will build a json object which we will pass to d3.js to visualize for us.
get '/visualization' do @user = current_user random_number = 20 + random.rand(31) @user.friend_matrix.sample(random_number).map{|fm| {"name" => fm[0], "follows" => fm[1]} }.to_json end
we are re-using the d3 chord visualization we saw before and that’s all there is too it.
you can try neosocial for your self at http://neosocial.herokuapp.com . as always, the full example application is available on github .
Published at DZone with permission of Max De Marzi, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments