I'm trying to create a Notification System without websockets & polling - Just simple JSON?

46
February 12, 2019, at 2:50 PM

I'm trying to build a real-time notification system without the use of Websockets or Polling, in the most efficient and fastest manner with no lags, and which costs the least UX.

This is the idea I drafted so far:

1- Each time a user (A) receives a message from another user (B), I use PHP to convey the messages between user A and B, and MySQL to store the transaction with a timestamp.

2- I run a Cronjob every 60 seconds to parse the MySQL data into JSON format, and update a file called data.JSON which stores all the messages between all the users on my website (@ stackoverflow security folks, there won't be any sensitive data or personal messages in the notifications if you're going to arrest me for that!). This means that the file is updated every 60 seconds whether there is new data to fetch or not. And this is an example the output in JSON format:

[
{
  "notification": {
      "user_ref": "00007939786",
      "sender": "12089301923",
      "message": "Message 1",
      "created_at": "2019-02-11 14:47:11"
  }
}, 
{
  "notification": {
      "user_ref": "00007939786",
      "sender": "78921739812",
      "message": "Message 2",
      "created_at": "2019-02-11 14:46:11"
  }
},
{
  "notification": {
      "user_ref": "00007939787",
      "sender": "91290381920",
      "message": "Message 3",
      "created_at": "2019-02-11 14:34:11"
  }
},
...

3- The webpage (eg. index.html) also has a JavaScript setInterval function which checks for changes in the JSON file every 60 seconds (the same amount of seconds as the Cronjob). This is how the jQuery code for reading the JSON file looks: https://codepen.io/mslilafowler/pen/xMjZmo?editors=0012

To summarize, the function is trying to check if the file has changed at all, and if the file changed, check if there's any notifications for the current user, and if there are, check if they are new (i.e. within the past 60 seconds, to be consistent). If all these conditions are met, then count the number of new notifications, and then print out the new messages (for testing purposes everything is done in the console).

I've done all this in theory, and have worked with parsing MySQL data into JSON format before, so that's well and good, however I'm faced with a couple issues:

a) This cannot work in Real Time as it is. I searched the entire web on synchronizing MySQL and JavaScript date-times-minus-60-seconds, but I finally reached the burnout stage because

b) PHP is asynchronous, tried using a mixture of Ajax and setInterval to get the actual current real time without page refresh. And this is causing my code to break, since it's checking if there are new notifications within the last 60 seconds, and if the user did not refresh the browser for many minutes, then the current PHP time is the same since the page was initiated, and then the code will reflect the old notifications as 'new'.

c) JavaScript's datetime is so different to MySQL's that I need to use an alternative library like mentions.js just to compare it without the timezones breaking up

d) All of this needs to be done without a page refresh

e) I looked into Websockets, Polling, and Long Polling, but would really prefer if this could be done with javascript/JSON, PHP and MySQL alone.

So I'm nearly there in creating a notification system, except for the mixup of times.

Firstly, each language has it's own time format and timezone, and secondly, the more accurate of the languages is asynchronous, which means that although PHP is more accurate, I can't get the current PHP time without refreshing the page every 1 second.

I feel like the only real solution is to get the returned result from an Ajax call inside a JavaScript setInterval function, something like setInterval(function(){ajax-get-PHP-current-time}, 1000), but my attempts failed hopelessly.

This question does not require opinionated answers, I just need advice on whether this method is okay, and a suggestion on how to synchronize the times between MySQL data, PHP, and JavaScript, without page refresh, in real time so that the old notifications don't get added into the new-within-60-seconds notifications.

READ ALSO
Mysql Select query to get data from three table

Mysql Select query to get data from three table

I have three table like below Table name : word

13
How to fetch data from two join tables with same database column names in codeigniter?

How to fetch data from two join tables with same database column names in codeigniter?

There are two tables 'student' and 'parent'both tables have 'f_name' and 'l_name' columns

23
PHP empty session files generated

PHP empty session files generated

I have a site where I use session authorization for my users Details are:

40
Change PDO login script to MySQLi

Change PDO login script to MySQLi

I have a problem because I don't know how I should change the ready-made login script with PDO to MySQLi ? Rest of my website is coded in MySQLiBelow its my config file

17