NoSQL Zone is brought to you in partnership with:

Max De Marzi, is a seasoned web developer. He started building websites in 1996 and has worked with Ruby on Rails since 2006. The web forced Max to wear many hats and master a wide range of technologies. He can be a system admin, database developer, graphic designer, back-end engineer and data scientist in the course of one afternoon. Max is a graph database enthusiast. He built the Neography Ruby Gem, a rest api wrapper to the Neo4j Graph Database. He is addicted to learning new things, loves a challenge and finding pragmatic solutions. Max is very easy to work with, focuses under pressure and has the patience of a rock. Max is a DZone MVB and is not an employee of DZone and has posted 60 posts at DZone. You can read more from them at their website. View Full User Profile

NeoSocial: Connecting to Facebook with Neo4j

08.19.2012
| 3264 views |
  • submit to reddit

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
               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, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)