dongmaobeng7145 2017-06-10 06:55
浏览 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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100