Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real time ajax comments

I plan to make this commenting system where comments can be posted and updated without refreshing the page ( you can see this on youtube)

Posting comments is understandable ( post to php page from javascript and run SQL query on server side to import it, than return the comment and fetch in html )

Updating is the part I don't understand. How can I refresh the page automatically on certain intervals and add comments? Isn't that going to be a mess when multiple users try to comment at the same time?

I was wondering if anyone can recommend a good way ( just like word of advice ) to achieve this and save me some time

like image 594
Alexim Avatar asked Oct 27 '12 01:10

Alexim


1 Answers

The most common way is to call setTimeout or setInterval in javascript to poll every 5-25 seconds. Basically, you store the idea of the last comment you received on the javascript side, then you call a function that sends this id to a remote server. If there are newer messages than this id, you send all of them back via XML or JSON (usually json is easier to deal with on the javascript side, especially if you use a framework like jQuery).

like image 150
ajacian81 Avatar answered Sep 29 '22 05:09

ajacian81