Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Facebook notify and instantly shows new comments or how does Stackoverflow do it?

I am a PHP developer and the title basically says it all. However I was hoping on some more in-depth information as I am starting to get confused about how the flow for the project I work on should go.

For an (web) application I need to implement a feature like Facebook does it with notifying users about replies/comments and instantly showing these.

I figured I could use long-polling with ajax requests but this does not seem to be a nice solution as the notifications never really are instant and it is resource heavy.

So I should use some form of sockets if I understand correctly, and Node.Js would be a good choice. So based on the last assumption I now get confused about the work flow.

I thought about two possible solutions:

1) It seems to me, that if I would use Node.Js I could skip using PHP at all and base the application on Node.js only.

2) Or I could use PHP as a base and only use Node.js for notifying users and instantly showing messages but saving the data using PHP and Mysql.

These two possibilities confuse me and I can't make up my mind about what would be the "best" and cleanest way.

I do not have much experience in Node.js, played with it for a while. But managing and saving data seems to be hard in Node.js so that is why I came up with option 2.

I know Facebook is build on PHP so I am assuming that they save the data via PHP and notify / instantly show replies and comments via Node.

Could someone help me out on this?

Thanks in advance!

EDIT: I just noticed, Stackoverflow does something similar. I get a notification in the upper left, and below my question a box with "new answer to this question". I am really interested in the technologie(s) used.

like image 593
Vincent Cohen Avatar asked Jan 30 '13 10:01

Vincent Cohen


People also ask

Why is Facebook notifying me of people's comments?

If you comment on a post, post notifications will automatically be turned on. If you turn on notifications for a post, you'll get notified whenever someone comments on the post. You won't get notified when someone replies to a comment.

How to tag someone in stackoverflow comments?

Your comment must include @username that you are referring to, where “username” is a reasonable match to the user's current display name (as seen in the comments above yours). There must be a starts-with, case insensitive match of at least THREE characters to the displayname.

How to mention in stack Overflow?

If you just want to mention a user in your question, you should link to their profile. If you're trying to get a specific user to answer your question, that's not a feature of Stack Overflow. Ask the question of the whole community.


2 Answers

Well you could use node.js for the notifications and PHP for your app. By googling I found this about real-time-notifications. You could also just use node.js with socket.io, but this means that you have to learn new technologies as you mention that you have no experience with node.

I haven't used it but you could check this project, for websockets in PHP.

When you have an update that you want to notify users you can use the publish subscriber pattern to notify the intrested in this update. Take a look in Gearman too.

Personally, I've built a notification system using the pubsub mechanism of redis, with node.js+socket.io. Everytime that there is an update on a record then there is a publish on the appropriate channel. If the channel has listeners then they will be notified. I also store the last 20 notifications in a Redis list.

The appplication is built in PHP. The notification system is built in node.js. They are different applications that see the same data. The communication occurs via redis. For example in the Facebook context: 1) A user updates his status. 2) PHP stores this to the database and Redis 3) Redis knows that this update must publish to the status channel of the specific user and it does. 4) All the friends of the specific user are listening to his status channel (here comes node.js) 5) Node.js pushes the notification in the browser with socket.io

As for facebook, I have read in an article that is using long polling for supporting older browsers. Not sure for this though, needs citation...

like image 169
x_maras Avatar answered Nov 05 '22 17:11

x_maras


AFAIK It would be via two simple methods :

  1. First one that could be very simple is adding a Boolean column to each record that determines if it has been notified or not.

  2. The second method is creating a table to insert all notifications.

However, I'm not sure if there are alternative methods for better performance, But first method is what I do commonly myself. But I think Facebook is using 2nd method, because it has to notify each one to a lot of users.

Your question maybe dublicate of:
Facebook like notifications tracking (DB Design)
Database design to store notifications to users

like image 1
Omid Avatar answered Nov 05 '22 18:11

Omid