Build Your Own Social Network with Diaspora*: An Introduction

Ahmed Refaat Tawfik

What’s Diaspora*?

Diaspora can be viewed from two different perspectives: first, from an end user’s, who could find Diaspora much similar to other social networks like Google+, Facebook, or even Twitter. I used to think that Diaspora was different, having some unique features until Google+ came and included many of these features.

The other angle sees Diaspora is from a developer’s perspective, as a Social Networking Engine. It is possible to build a social network with sophisticated features and a very simple and elegant UI in few weeks, or even days, depending on your experience.

Why Diaspora?

Diaspora is open source and distributed which means you could connect your deployed instance with other deployed instances and make a huge social network fully connected, all while maintaining your data on your servers. Also, it’s written in Ruby on Rails and the UI uses Backbone.


With the introduction out of the day, let’s see Diaspora in action. First, you need to install some dependencies:

Ruby: you can find all you need in this tutorial Installing Ruby with RVM on Ubuntu

Rails: these two tutorials are helpful for installing rails Rails Intro, Deep Dive: Installing Rails, Part One, and this one Loccasions: Installing Rails Part 2

MySQL: at least for development purposes, you will need MySQL so this commands will install it for you and to run MySQL server
[gist id=”1975336″]

Git: you need git to clone the diaspora repository, despite that you can download it from the repository page but you will need git anyway, so you have to install it, and if want to setup git to work with your github account, you should read this also from github help pages for linux
[gist id=”2017154″]

Diaspora: now you need to get diaspora source to work with, so we going to clone it from the project git repository and switch to the diaspora directory
[gist id=”2017174″]

You almost now have a working environment to run Diaspora, but we have just a few steps before running the server.

Run the following command from the root directory of diaspora project itself. For example, if your project was in your home directory, the root directory of the project will be at ~/diaspora/
[gist id=”1975392″]

Now, go to the config/ folder and remove the [.example] from the following files:

  • database.yml.example
  • application.yml.example

In database.yml, you need to configure MySQL server entries to match your environment.

Last commands to start the project are
[gist id=”1975425″]

Open your broser and go to [ localhost:3000 ] and follow the directions to create a user. If you want this new user to have admin privileges, you can edit that in config/application.yml by adding the username to the admins section. You can actually do a lot of configuration in this file, which we will discuss in the next tutorial.

Diaspora Codebase

The typical Disapora code directory structure is below. Take a moment to familiarize yourself with the code base.

Let’s start our familiarization with the Diaspora object model. You can find the source for these objects in the app/models folder.


A User object represents the private information and capabilities of a user on that server. The user object is able to friend people, post updates, and update his profile. A User has a Person.


A Person is a User viewed from the outside. When a user friends another user, they friend that user’s Person object. Person objects are replicated across servers, and they are where a User’s public key lives. A Person has many Posts. A Person has a Profile.


This contains information about the person. Currently, a profile looks the same to anyone looking at it.


A Contact is a “proxy” object for every person a User is friends with, means that to initialize any relationship with any other user you need a contact object to make the link.


This contains a list of people and posts which are for that aspect. Aspects are private to Users, and we might embed the Aspect documents in the User document.


A Post belongs to a Person. This is a parent class for different types of posts, it contains comment ids and a few other attributes common to all Posts.


A comment belongs to a Post

Directory Structure

Here is what the code base for our Diaspora* app looks like:

├── app
│   ├── controllers
│   │   ├── activity_streams
│   │   │   └── photos_controller.rb
│   │   ├── admins_controller.rb
│   │   ├── apis_controller.rb
│   │   ├────────────
│   │   ├── people_controller.rb
│   │   ├── photos_controller.rb
│   ├── helpers
│   │   ├── application_helper.rb
│   │   ├── aspect_global_helper.rb
│   │   ├────────────
│   │   ├── error_messages_helper.rb
│   │   ├── getting_started_helper.rb
│   ├── mailers
│   │   ├── diaspora_devise_mailer.rb
│   │   ├── notification_mailers
│   │   │   ├── also_commented.rb
│   │   │   ├── base.rb
│   │   ├────────────
│   │   │   ├── reshared.rb
│   │   │   └── started_sharing.rb
│   │   └── notifier.rb
│   ├── models
│   │   ├── account_deleter.rb
│   │   ├── account_deletion.rb
│   │   ├── tag_following.rb
│   │   ├────────────
│   │   ├── user_preference.rb
│   │   └── user.rb
│   ├── presenters
│   │   └── user_presenter.rb
│   ├── uploaders
│   │   ├── processed_image.rb
│   │   └── unprocessed_image.rb
│   └── views
│       ├── admins
│       │   ├── _admin_bar.haml
│       │   ├── correlations.haml
│       │   ├── stats.html.haml
│       │   ├── user_search.html.haml
│       │   └── weekly_user_stats.haml
│       ├────────────
│       ├── apps
│       │   └── show.html.haml
│       └── users
│           ├── edit.html.haml
│           ├── getting_started.haml
│           ├── logged_out.haml
│           └── privacy_settings.html.haml
├── Capfile
├── config
├── db
├── lib
├── log
├── public
│   ├── images
│   ├── javascripts
│   │   ├── app
│   │   │   ├── collections
│   │   │   ├── helpers
│   │   │   ├── models
│   │   │   ├── templates
│   │   │   └── views
│   │   ├── helpers
│   │   ├── pages
│   │   ├── vendor
│   │   └── widgets
│   ├── stylesheets
│   └── uploads
├── script
├── spec
└── tmp

Okay, what’s next?

We only covered the very basic of Diaspora. There are many more pieces to the puzzle, like Controllers, Views and the Backbone part of the UI which you can find in the public/javascript/app. It will your mission, should you choose to accept it, to take a look over them until the next tutorial, where we will customize Diaspora to build our social network with some specific features.

CSS Master, 3rd Edition