dongmaobeng7145 2017-06-10 06:55 采纳率: 0%
浏览 34
已采纳

由于通知轮询,页面加载速度非常慢

I am working on notification system, but my pages load very slow due to polling of notification. I mean very slow, but when I comment out notification code then it runs smoothly. Here is my ajax code for polling notifications :

function pollNotification() {

$.ajax({
    method: 'POST',
    url: urlGetNotification,
    async: true,           
    timeout: 0,         
    cache: false,
    data: {
        _token: token
    },

}).done(function (notifs) {
    //my code here

}).always(pollNotification);
}

Here is my server side php(laravel framework) code for fetching notifications :

public function getNotification()
{
    $count=0;
    $user = User::select('last_notif_timestamp')->where('id',Auth::user()->id)->get(); // fetching last timestamp when user clicked on notification

    $notification = Notification::where('receiver',Auth::user()->id)->orderBy('updated_at','desc')->get(); //checking for notification in table
    $prevDate = Session::get('prevDate'); //temporary variable to check when the last notification came

      if($notification->count()>0) {
          if ($prevDate == null || $prevDate < $notification[0]->updated_at) {
              Session::set('prevDate', $notification[0]->updated_at);

              $notifications = array();
              foreach ($notification as $notif){
                  if($notif->updated_at > $user[0]->last_notif_timestamp) //Keeping track of notification counter
                      $count++;
                  $notifications[] = $notif;
              }

              return response()->json(['notifications'=>$notifications, 'count'=>$count],200);
          }
          else{
              sleep(10); // Sleeping for 10 seconds for next poll
              self::getNotification(); //calling function recursively
          }
      }
    sleep(10);
    self::getNotification();
}

In short this code checks for notification and if new notification are present then it returns those notifications with count value. If there are no notification then it sleeps for 10 seconds and calls the same function recursively.

Please suggest any solution to load the pages faster. Thanks!

  • 写回答

1条回答 默认 最新

  • dta43039 2017-06-10 14:19
    关注

    Why do you want to wait on php side?

    Currently you force your php script to wait before sending a response to you ajax request and this wait can be very long and memory consuming for the server with the recursive calls.

    You can do a simple polling on the Javascript side using setTimeout(), for example. On the php side, you can just return the notification array even if it is empty (check it using Javascript) without any sleep(). This should at least reduce the response time of your ajax request and maybe the loading time of pages.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择