A minimal Twitter clone, built with Node and Redis.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
bin
lib
public
routes
test
views
.gitignore
.jshintrc
.travis.yml
Procfile
README.md
app.js
package.json

README.md

twitter

Build Status

A minimal Twitter clone, built with Node and Redis.

Live demo

https://twitterlikeapp.herokuapp.com/

Features

Currently the following features are implemented.

  • Sign up and log in
  • Follow and unfollow user
  • Post tweet

Data layout

Users

User IDs are generated sequencially by Redis's INCR:

INCR user:ids => 123

Each User is stored in Redis Hashes. The Redis key of user is something like user:123 and fields are:

name (User name)
pass (Encripted password)
fullname (User's full name)

Every time someone registered, this app execute the following steps.

INCR user:ids => 123
HMSET user:123 name "bob" pass "asdfjkl;" fullname "Bob Marley"

Followers and followings

For followers and followings, Redis's Sorted Set data structure is a good fit because whenever someone start following you, or you start following someone else, that history is always sorted by insertion order (the timestamp) using the score of Sorted Set.

user:123:followers => Sorted Set of user IDs of all the followers users
user:123:followings => Sorted Set of user IDs of all the follwings users

When adding new follower:

ZADD user:123:followers 1401267618 456 =>> Add user 456 with time 1401267618

Tweets

Tweets also have sequencial IDs, generated by Redis's INCR which is same as User ID.

INCR tweet:ids => 789

Every time someone tweets, that tweet is stored in a hash, where each key is something like tweet:789, with the following fields:

text (Body of the tweet)
created_at (Timestamp created)
user_id (User ID)

Timeline

Another interesting part of this application is the Timeline. where users see all the latest updates in user's home page. Since we show those updates in chronological order, from the most recent update to the oldest, again, the Sorted Set data structure is the good fit for this use.

The app needs to create two Timelines, one is home timeline where all new users will see, and the other is user timeline where all updates are customized per user.

user:123:user_timeline => Sorted Set of tweet IDs posted by the user
user:123:home_timeline => Sorted Set of tweet IDs posted by the user and the users they follow

For example, retrieving latest 5 tweet IDs from the user timeline is:

ZREVRANGE user:123:home_timeline 0 4

Development

Download project

Clone this repository and cd to the project's home directory, run:

$ npm install

This will install all required dependencies.

Install Redis

For Mac OSX, the easiest way would be to use Homebrew.

$ brew install redis

Otherwise, please look for other options in Redis's official documentaiton.

Start application

This application is based on the web application framework called Express. To start the application, run the following command:

$ npm start

It will serve the application at http://localhost:3000/.

Deployment

For deployment, Procfile is packaged as a default configuration to integrate with Heroku. If you are not familiar with Heroku I would recommend you to first read through the documentation of how to get started with Node.js on Heroku.

The whole deployment process should be as simple as the follwing 3 steps.

Login to Heroku:

$ heroku login

Create an app on Heroku:

$ heroku create

Deploy an app:

$ git push heroku master

References

  • antirez/retwis - A Twitter-toy clone written in PHP and Redis
  • twissandra/twissandra - Twissandra is an example project, created to learn and demonstrate how to use Cassandra.

License

MIT © Tatsuya Oiwa