NeoSocial: Connecting to Facebook with Neo4j

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:

  1. A web service that handles authentication and displaying of friends, likes, and so-on.
  2. 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
               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.

Tagged , , , , , , , ,

23 thoughts on “NeoSocial: Connecting to Facebook with Neo4j

  1. [...] NeoSocial: Connecting to Facebook with Neo4j by Max De Marzi. [...]

  2. Ravicious says:

    Well, I tried it and something went wrong.

    • maxdemarzi says:

      Hi Rafal,

      If you give it a few minutes all your facebook friends will load. They should already be there by now.
      There was a flood of users yesterday due to Reddit and the import jobs were taking longer than normal.

      Thanks for trying it out.
      Max

  3. Gustav says:

    do you have a smiliar project/tutorial connecting twitter? I would like to follow tweets etc…

  4. Is there a library to produce the cool visualization shown at the top of this page?

  5. Abdul Azeez says:

    How is redis used here? Can you please provide information on that as well?

  6. David says:

    Hi Max, congrats for your work.

    Could you please share the data model (json) for the chord?

    Regads

    • maxdemarzi says:

      You can see more about how the chord is made on Graph Visualization and Neo4j – Part Three. The json result looks like:

      [{"name":"Alejandra Stace Torres","follows":["Jose Pardo","Gabriela De Marzi","Vicky Pardo de Vargas","Silvana Vargas","Max A De Marzi","Cecily Vargas","Anahi Plenge","Luis Pardo","Alex De Marzi","Karen Plenge","Hortencia Ratto","MariaCarmela Pestana Gayoso"]},{"name":"Michael Gill","follows":["Helene Astier","Ian Wilson","Gary Lutchansky","Rob Asfar","Kurnia Kosasi","Richard Champalbert"]},{"name":"Gary Lutchansky","follows":["Helene Astier","Ian Wilson","Rob Asfar","Kurnia Kosasi","Michael Gill","Richard Champalbert"]}......

  7. HisHighnessDog says:

    Max, I am having some problems getting it to run.

    My configuration details:
    I confirm that I have both an “APP ID/API Key” and an “APP Secret” (checked at my Facebook “App Dashboard”; https://developers.facebook.com/apps).

    Symptoms (seen at both local install and Heroku deployed instance):
    (1) If I click on “Sign in with Facebook”, the error message reads:
    {
    “error”: {
    “message”: “Missing client_id parameter.”,
    “type”: “OAuthException”,
    “code”: 101
    }
    }
    (2) Navigating to /visualization, the error message reads:
    “NoMethodError at /visualization”
    “undefined method `friend_matrix’ for nil:NilClass”…
    No GET data
    No POST data…
    file: neosocial_app.rb location: block in line: 71″
    Here is line 71:
    @user.friend_matrix.sample(random_number).map{|fm| {“name” => fm[0], “follows” => fm[1]} }.to_json
    _____________________________________________________
    Q: Does one need “User Token” and “App Token” described on Facebook’s “Access Token Tool” page (https://developers.facebook.com/tools/access_token/) to get this example running?

    • maxdemarzi says:

      In this section:


      export SESSION_SECRET=
      export FACEBOOK_APP_ID=
      export FACEBOOK_SECRET=

      Make sure that you are setting it right:


      export FACEBOOK_APP_ID='1234' # does not work
      export FACEBOOK_APP_ID=1234 # should work

      • HisHighnessDog says:

        Thanks for the reply. I double-checked and did as noted above… it didn’t work. I also tried cycling the “App Secret” (?same as FACEBOOK_SECRET? above)… still no luck. Any other thoughts?

  8. Xi Chen says:

    When I use “git push heroku master”, I received the following error:
    “Heroku push rejected, no Cedar-supported app detected”. Does someone know the reason? I tried troubleshooting by deleting Gemfile.lock file, but still the same problem.

  9. Marko says:

    Very nice tutorial, great idea to use neo4j and facebook and visualize the connections. Unfortunately it somehow tells me I got 0 likes and 0 friends so I dont see the visuals. Is there a caveat on my side like facebook limitations to the app or privacy settings or is there an overload on your side? thanks and great work.

  10. Great article! Unfortunately it seems that the access to the facebook graph doesn’t work anymore.
    I’ve waited almost 10 hours, now, but it I’m still kind of unpopular ;-)
    My friends are still 0.

    Are you aware of any issues at the moment?

  11. […] you can get recommendations that your friends like, but that’s pretty trivial and we’ve seen how to do that with Neo4j already. Add food pictures from Foursquare, and menu data from single platform and […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 1,680 other followers

%d bloggers like this: