Over a million developers have joined DZone.
Platinum Partner

NeoSocial: Connecting to Facebook with Neo4j

· Java Zone

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.

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

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)

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"]

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)

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"])


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)

        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]
      batch_result = $neo_server.batch *commands

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"]

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

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.

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.


Published at DZone with permission of Max De Marzi , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}