doutui2016 2019-02-11 14:25
浏览 56

我正在尝试创建一个没有websockets和polling的通知系统 - 只是简单的JSON?

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 matlab中使用gurobi时报错
    • ¥15 WPF 大屏看板表格背景图片设置
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂