But for some reason it keeps giving me the same reactions over and over again, in other words every 5 seconds the function is appending the same messages again. What am I doing wrong?
It looks like you’re comparing the date of the last request with the dates when the responses were created… and since that latter date won’t change, you get the same responses over and over again. Instead, you might store the ID of the latest response from the last request in a session variable (or just send it back to the server as a request parameter), and subsequently only query for responses with an ID greater than that.
@m3g4p0p. Thank you very much for the reply. This is driving me insane . I am looking for hours now. Everywhereelse I see people using timestamp but I can’t get it to work in my situation. How would it work with what you suggest, can you please give me an example
Right. Your problem is that your database is storing values in YYYY-mm-dd HH:MM:SS format, and your are comparing against the result of time() which is an integer.
Curiouser and curiouser. I honestly don’t know what’s going on at this without testing myself, and I’m afraid I don’t have time for that the next few days.
if (isset($_GET['last_id'])) {
$sql .= ' WHERE response_id > :last_id';
$params['last_id'] = $_GET['last_id'];
}
… and in your JS you send the last response ID (or actually greatest, if ordered otherwise) in the query string. Alternatively you might store the last response ID in a server-side session variable.
I just want to mention that polling is not the only way.
You could also set your PHP Server up to actively push new messages to the client. (E.g. Ratchet: http://socketo.me/)
Then you would save the overhead of actively asking for new messages.
However, as I see it, your problem is something else.
Please find out what you really get from your DB.
What is lastChecked?
Run the SQL Statement manually, if you verified that you get the correct data.
What do you get for SELECT * FROM post_responses WHERE response_date > 1528700000?
Try to work it out from there.
Hope that helps.
You can use Chrome DevTools to see what you are sending to the server and what you can get back. Make screenshots from the request and response data. Also: Make sure you actually get JSON, send JSON. etc.
@MartinMuzatko. Thank you very much for the reply. For sure I’m goig to have a look at Ratchet: http://socketo.me. I think I found the problem with the polling. I am still testing, but if I know I will let you know.