Best way to do long polling?

I have been reading article after article this afternoon, but I am not sure how to approach the thing I want. Let me explain!. I have a blogpost page where the owner is publishing short articles about paragliding (tbl. blog_posts). Visitors can post comments to those articles (tbl blog_post_comments). What is the best way to poll the database for new comments and what should be the structure of tbl blog_post_comments.

edit: Should I have a certain feld in the tbl blog_post_comments to check for? Because most articles I was reading so far ara polling every x seconds, which is ok when there are not a lot of visitors, but the server is becoming overloaded when there are many visitors I guess. and basiccaly I just want to poll when something has changed in tbl blog_post_comments. It would be so good when someone finally would write an article or tutorial that is complete, incl, database interaction

Thanks a lot

Basically you have three options here:

  1. Poll with interval - every 30 seconds or so, fire an AJAX request that asks if any new posts were added after the comment ID of the last received comment. Unless you have a lot of visitors this isn’t really intensive and is the easiest option to build

  2. Long poll - Send an AJAX request and on the server side keep waiting until something is added to the database, i.e., query, sleep(1), query, sleep(1), query, etc. Then once the AJAX gives a response, apply it and fire a new AJAX request. When the AJAX request times out, also send a new AJAX request. This is a lot more intensive than option 1 because you need to keep a connection open for all users all the time, also you’re querying a lot.

  3. Websockets - Something entirely different than AJAX. A connection is opened from the browser to the server and will stay open until the user browses away. What you can do here is have it listen to some push/pull mechanism like Redis or RabbitMQ and also publish new comments to that, and every time a comment comes in push it to the client. This is the most real time of all three options, but requires an additional pub/sub component and also requires an open connection for all visitors.

Given those three options I’ll let you decide which one suits best for your use case :slight_smile:

1 Like

@rpkamp. You know what my problem is? I have no idea where to start. That’s why I stated about a good tutorial included on how to query the database. How do I check for any new posts? do I need two separate functions for that and how do I implement that in AJAX. Can you illustrate it with a simple example?

THank you in advance

If you’re going to use websockets, I recommend, it had libraries for both your server and your client. It is not based on AJAX or Fetch, it is it’s own thing. Their website should have all the info you need to get started.

1 Like

@mawburn. Thank you for the reply. I actually would like to go for the first option @rpkamp gave me but, like i said in my reply. I have know idea how to check for the last aded Comment ID like Scallio suggested

yeah, a heartbeat is a valid alternative to websockets. How you pull the latest ID is entirely dependent on your backend and doesn’t really have anything to do with JS.

@mawburn. Thanks again for the reply. I understand what you say. But still I need to send data from JS to my backend (php) in order to pull thta latest id, or am i wrong. I’m just very confused. I can’t make the circle round

You need to make a call to your api endpoint that will respond with the data you need. It should be built just as any other endpoint you might have.

An api endpoint is just like a normal page, but instead of responding with html it responds with json data only. If you’re having trouble with this, then you might need to ask in PHP with information about how your backend is built currently.

1 Like

@mawburn Thanks for the reply. Very much appreciated

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.