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

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


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...


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


You could use Server Side Events it involves a bit of JavaScript but nothing overly complicated I think.

The main bulk of this method is PHP though, so you would just use the PHP to query your DB for notifications and SSE will push them to the user.

It does have some limitations though, most notably it's not supported by IE (huge surprise) thought i'd mention it anyway to let you know of other possibilities.

Hope this helps