What is the difference between long polling and normal ajax?

3

I'm trying to understand more about long polling to "manipulate" a site in real time, I've seen some videos and I'm thinking so far:

Let's say I have an old date that is in the sql and I echo it.

How will long polling know if the old date will not be the same as it will fetch from time to time according to the setInterval function ...?

Let's say I want to post a blog post in which all the text is in mysql, but of course I publish a new post, and whoever is on the page at the time, will see the post in the hour (ava), then how a long polling code will know the difference between the old and the new publication? Even not to conflict or repeat the same date recorded in sql.

Remembering that I do not know anything about long polling, then I may be asking nonsense ...

    
asked by anonymous 15.05.2015 / 07:48

1 answer

1

To use long-polling in this specific context, the ideal is the page that makes the long polling receive a parameter that corresponds to the last timestamp that the user has.

With this, the page will query the database to see if there are more recent posts, and if they exist, it returns the information.

On the client / browser side, when information is received from the long polling page, the content of the html is refreshed with the new information received, and the long polling page is called back with the timestamp of the information if you just received it.

I hope the text is not confusing:)

The difference is that instead of always being ajax client-side requests for new posts, you make an x in x seconds or minutes, and then the request gets server-side checking to see if there are any new posts, and returns results if they appear.

    
15.05.2015 / 11:56