Coder Social home page Coder Social logo

msveshnikov / twitter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tatsuya/twitter

0.0 2.0 0.0 792 KB

A minimal Twitter clone, built with Node and Redis.

Home Page: https://twitttter.herokuapp.com/

JavaScript 70.63% CSS 3.87% Pug 24.71% Dockerfile 0.77% Procfile 0.02%

twitter's Introduction

twitter

Node.js CI

A minimal Twitter clone, built with Node and Redis.

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 directory, run:

$ npm install

This will install all required dependencies.

Install Docker

This project uses Express as a web application framework. The Express app is containerized with Docker enabling you to run the Node.js application and Redis in containers.

To build and run the Node.js application, you can use Docker Compose commands. Compose is a tool for defining and running multi-container Docker applications. With Compose, you can create and start the application and dependent middleware with a single command.

From the project directory, start up the application by running:

docker-compose up

Docker Compose pulls a Redis image, builds an image for the application code, and starts the services defined in docker-compose.yml file.

Enter http://localhost:49160/ in a browser to see the application running.

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

twitter's People

Contributors

tatsuya avatar msveshnikov avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.