i have implemented a simple and raw chat system, in php and ajax. The basic functions are, to add new contacts and have a conversation with them, basicly like telegram or whats'app. So, i have a lot of chats, and now i will implement a feature that let to notify new message to the contacts. I read something about this, but i didn't understand a lot. How to make that, i read about long polling or websocket but this works only with new browsers. Now it implement this very bad, with a function in ajax, every second it sends a request to server and reload all messages in current chat, but not the other chats, and didn't can visualize a notify. Please help me :DD
Asked
Active
Viewed 900 times
-3
-
1This is way too broad, the only thing we can give you is a basic idea of what do to. But the general idea is have an observer periodically check for changes in the database (*such as seeing if the COUNT of records is changed*) and send the update when there is one. – Spencer Wieczorek Feb 28 '17 at 23:41
-
thanks for answer, ok so you saying i need a function that for example evrery second see if something chancing in database, and depending on what's changed, i do something, in this case send notification? – samuele Feb 28 '17 at 23:52
-
I've added an answer (*since it's too long for a comment*) that explains the general approach to do what you want, hopefully that helps and you can implement that approach in your application. – Spencer Wieczorek Mar 01 '17 at 00:04
1 Answers
0
The general idea on how to do this is have an observer periodically make a call to the database to see if there was a change. You can use AJAX for this and call that function such as setInterval(myAjaxFunction, 3000)
for example to check every 3 seconds.
For checking the database one approach would be to look at the record count in the table and store that initially. Something like:
SELECT COUNT(*) FROM tableName
To get the number of records in a table. So do this initially and store that in JavaScript, then have the return of AJAX send the value of the query. Then you could do something like:
if(queryResult != currentRecordCount){
currentRecordCount = queryResult;
sendNodification();
}
In the success
after the AJAX call.
This together will make an observer that checks every 3 seconds, if something changed in the database it will call a sendNodification
function.

Spencer Wieczorek
- 21,229
- 7
- 44
- 54
-
yes, fanstastic i made something like that, but with this function, i can't now which row changed, and i can't send notify to specific user!! 'cause I want send notify to specific user that received a message! Thanks really for answer – samuele Mar 01 '17 at 00:05
-
@samuele Are your IDs auto incrementing integers and just `INSERT`s? If so you select the new records by their ID and use `WHERE (ID > queryResult - (queryResult - currentRecordCount ))` instead assuming `queryResult > currentRecordCount`. Dose the record have information about which user this goes to? If so you would want to get that as well in the query (`SELECT user FROM TblName WHERE ...`). – Spencer Wieczorek Mar 01 '17 at 00:14
-
A more brute force approach about have a copy of the table on the client and get the table from the database and loop through and check for differences, but that's more burdening on the server to pull an entire table. But if there are relatively small amount of records this will be ok to do. – Spencer Wieczorek Mar 01 '17 at 00:17