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