Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Notifications in Rails

In my application, I want to notify a user, when he/she is mentioned in a comment or a post.
The user handle is @user_name, similar to Facebook.
The database table for mentions looks like:

Mention
  mentioned_by: user_id (foreign key)
  user_mentioned: user_id (foreign key)
  comment_id: (foreign key)
  post_id: (foreign key)

I can't figure out a way to implement it though. How do Facebook / Twitter do it?

What I decided to go with, was use ActiveRecord callbacks/ Observer design pattern and whenever a new comment/post is saved to database, I can go through the contents of the post/comment, and look out for any mentions and then execute the notifications as required.

I get the feeling that there are some missing pieces and I am not getting it right.
Is this the best way of doing it?

like image 509
Jatin Ganhotra Avatar asked Jul 21 '11 06:07

Jatin Ganhotra


2 Answers

Facebook and Twitter are not mid-size Rails applications. They are companies. The tech that runs them is distributed and mostly custom, especially in the case of Facebook.

The part that you seem to be grasping for is how they determine who to notify in a performant and scalable way. This is where shit gets real. You can find a lot of information about the architecture behind each on of them, and there is certainly a lot of great stuff to help you think about these things, but ultimately none of it is going to be something you implement into whatever application you're building.

http://www.quora.com/What-is-Facebooks-architecture

Facebook Architecture

http://www.infoq.com/news/2009/06/Twitter-Architecture

http://engineering.twitter.com/2010/10/twitters-new-search-architecture.html

Plenty more juicy details over at Quora.


Unfortunately, none of this gets you closer to your goal. I think the most realistic thing for you to do to start out with woud be to simply tie in a service like Pusher to send messages to clients without worrying about it, use an ActiveRecord Observer to add notifications to a background queue where the workers actually send those notifications to Pusher. This is a day or less of work and it should scale well to at least 10k notifications a day. Once you start having performance problems, ditch the Observer and Pusher for something like Goliath that can handle both of the jobs locally.

Bottom line, learn what you can about large and experienced systems that have been put through the paces, but mainly just to see what problems they ran into and how they solved them. These methods aren't the same among the big guys even, and they are going to vary for each implementation.

Hopefully that helps point you in a good direction. :)

like image 113
coreyward Avatar answered Nov 04 '22 01:11

coreyward


You can use ActiveRecord callbacks while record is saved (like before_save, after_save or before_create, after_create) to go through comment content, find and create all mentions models and save them to db.

like image 3
Hck Avatar answered Nov 04 '22 01:11

Hck