0

I realised when testing my website, that a user that receives a message/comment has to refresh the page before they see any alerts or changes.

In comments.php?do=comment&id=1 shows the post with the id of 1 for users to comment on.

I use this form to submit a comment, and this is shown by a mysql fetch on the same page to show the content.

<form action="" method="post" > input name="getcomment" type="text" value="What are your thoughts?"/> <input value="Comment!" type="submit"/> </form>

I have seen quite a few resources online on updating the content automatically when a new comment has been submitted (for the recipient) but I am not sure which is best. Bearing in mind this is a very small scale website.

  1. What could be my options? (JQuery/Ajax seems the simplest) But is 'polling' the database inefficient?

  2. How would I implement this (which I am finding most difficult)

rp7
  • 139
  • 1
  • 1
  • 6
  • Take a look at [socket.io](http://socket.io), a framework you can use together with php to notify clients of events like this. For the second question I can link to another [answer](http://stackoverflow.com/a/6420431/208753) – Elger Mensonides Apr 06 '13 at 10:57
  • Thanks, I will look into that. What makes socket.io advantageous over other similar services? (if any). – rp7 Apr 06 '13 at 11:00
  • Well I personally use signalr, but that's for .NET. Basically signalr does what socket.io does, but socket.io is more mature. It's definitely more easy that it looks. – Elger Mensonides Apr 06 '13 at 11:01
  • Oh okay, yeah it does look slightly confusing but I will definitely read up on this – rp7 Apr 06 '13 at 11:04

2 Answers2

1

You just need to poll your php script every few second's like this. and Bearing in mind this is a very small scale website polling can do what you want.

setInterval(function(){
   $.ajax({
     url: "getComments.php"
   }).done(function(comment) {
     // show new comments
     $('#comment_section').append(comment);
   });
},5000); // poll every 5 seconds
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
  • Thanks! In the `'show new comments'` section, what would I put into there? The
    to refresh that holds my `SELECT` query?
    – rp7 Apr 06 '13 at 11:04
  • what do you mean by this _The
    to refresh that holds my SELECT query_
    – Adil Shaikh Apr 06 '13 at 11:06
  • So in my `comments.php`, I have the post that is being commented on. And below this is a `
    ` containing a `SELECT` query to relay the comments. It is this that needs to be updated automatically.
    – rp7 Apr 06 '13 at 11:08
  • No.. move that query part to another php `getComments.php` and simply poll that php with ajax. – Adil Shaikh Apr 06 '13 at 11:11
  • Just the query? Because I also use a `mysqli_fetch_array` following the query in `comments.php` – rp7 Apr 06 '13 at 11:16
  • I mean , the part that get's and display's the comments from database...... essentially.. all the code associated to comments should be moved to new php.] – Adil Shaikh Apr 06 '13 at 11:19
  • And so essentially, in `comments.php` will have an empty `
    ` called `#comment_section`?
    – rp7 Apr 06 '13 at 11:32
  • I got this working (kind of) however, it non-stop is posting over and over again the same content of the whole
    ! Not only recent comments but all from the database
    – rp7 Apr 06 '13 at 12:51
  • you need to manage a variable that holds the time when you last retrieved the comments form DB and every time you need to get comments just use `WHERE time > lastRetrievedTime` in your query, and update the variable every time you retrieve comments.... – Adil Shaikh Apr 06 '13 at 14:40
0

I think that your best options would be to use JQuery/Ajax as it is much cheaper alternative to using sockets.

IF it is not important for your users to receive immediate updates, you can just poll.

Seanbish
  • 41
  • 2