Note that there are some explanatory texts on larger screens.

plurals
  1. POReal time data updates with comet and PHP?
    primarykey
    data
    text
    <p>I'm looking to implement real time notification updates on my social networking website. I have done some research on comet and i'm really fascinated by it.</p> <p>From what I understand, this is the basic flow of what happens on a comet server.</p> <pre><code>Webpage: Sends an ajax request to server when the document is ready. Server: Queries the database every x amount of seconds and returns a json string containing results if any are found. Webpage: Receives the result of the json string from the server and sends out another ajax request to do the above process again. </code></pre> <p>By understanding the flow of how comet works, I've written some PHP and Javascript code.</p> <p>The JavaScript code uses the jQuery library and sends an ajax request out to the server with the current time in a unix timestamp format as a GET parameter.</p> <pre><code> $(document).ready(function(){ var timestamp = Math.round(new Date().getTime() / 1000); function comet2(){ $.ajax({ type : 'GET', url : 'comet.activities.php?timestamp=' + timestamp, async : true, cache : false, success : function(data) { alert("current timestamp "+timestamp) var json = JSON.parse(data); if(json !== null){ alert(data); } timestamp = json[0].timestamp; setTimeout('comet2()', 1000); }, error : function(XMLHttpRequest, textstatus, error) { setTimeout('comet2()', 15000); } }); } //call the comet function because the page has loaded. comet2(); }); </code></pre> <p>The PHP code will query for new activities by searching the database for new rows by using a timestamp paramater (in this case, a unix timestamp in a query). For this example, I have limited the amount of results to 1.</p> <pre><code>&lt;?php set_time_limit(0); include("models/config.php"); global $mysqli,$db_table_prefix; $last = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0; $results = null; $flag=true; $stmt = $mysqli-&gt;prepare("SELECT id,timestamp FROM uc_user_activity WHERE timestamp &gt; ? ORDER BY timestamp DESC LIMIT 0,1"); $stmt-&gt;bind_param("i", $last); $stmt-&gt;bind_result($id,$timestamp); while($flag){ $stmt -&gt; execute(); while ($row = $stmt-&gt;fetch()){ $flag = false; $results[] = array( "id" =&gt; $id, "timestamp" =&gt; $timestamp ); } $stmt -&gt; close(); usleep(100000); clearstatcache(); } echo json_encode($results); ?&gt; </code></pre> <p>The code above doesn't actually 'work' The problem is that if a user posts a new comment, it will fail to add to the database when the comet script is running. This means that the comet script will never return any json result because the statement in the sql query is never met (no new activities are added with a newer timestamp). My ajax code for posting new comments is working 100%, so I know that isn't the problem. Simply 'nothing happens', that is - nothing (no errors) are alerted or outputted to the browser console.</p> <p>Edit number 3: I'm seriously struggling to explain what I mean by 'nothing is happening', so I have uploaded an image showing that the database insert fails when the comet script is being called from jquery (notice how the textbox is disabled whilst the comment is being posted via ajax).</p> <p><img src="https://i.stack.imgur.com/NqGsS.png" alt="enter image description here"></p> <p>What can I do about this? I've spent hours searching the internet trying to fix this/find a similar working example with no avail.</p> <p>If I change the query in my PHP code to be:</p> <pre><code>$stmt = $mysqli-&gt;prepare("SELECT id,timestamp FROM uc_user_activity WHERE timestamp **&lt;** ? ORDER BY timestamp DESC LIMIT 0,1"); </code></pre> <p>instead of:</p> <pre><code> $stmt = $mysqli-&gt;prepare("SELECT id,timestamp FROM uc_user_activity WHERE timestamp &gt; ? ORDER BY timestamp DESC LIMIT 0,1"); </code></pre> <p>results are instantly alerted to the browser window, comments can be posted again and the script is called again and new posts are displayed. This shows that my code 'is working' fine afterall and it looks like the query is causing the problem... </p> <p><img src="https://i.stack.imgur.com/FgD04.png" alt="enter image description here"></p> <p>Can anyone see what is going on here? I have edited this question 7 times now and any guidance would be great as I'm just getting nowhere. </p> <p>Just so this doesn't get closed, here is my question to round up what I have discussed above:</p> <blockquote> <p>Are there any better ways of implementing a comet server? I'm not the most experienced guy ever, but I would really like to learn how to do this. It seems StackOverflow has this functionality and it works perfectly - how are they doing it?</p> </blockquote> <p>I can't possibly write my post in any further detail than this and I would REALLY appreciate some guidance from you awesome people. A suggestion as to why my code 'isn't working' or links to any tutorials explaining how to implement this would be amazing! Thanks in advance and apologies for this monster of a question and all of the edits!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload