简体   繁体   English

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

[英]Pages load very slow due to notification polling

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 : 这是我用于轮询通知的ajax代码:

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 : 这是我的服务器端php(laravel framework)代码,用于获取通知:

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. 如果没有通知,则它将休眠10秒钟,然后递归调用同一函数。

Please suggest any solution to load the pages faster. 请提出任何解决方案以更快地加载页面。 Thanks! 谢谢!

Why do you want to wait on php side? 为什么要在php端等待?

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. 当前,您迫使php脚本在发送对您的ajax请求的响应之前等待,并且这种等待可能非常长,并且对于带有递归调用的服务器来说,这会占用内存。

You can do a simple polling on the Javascript side using setTimeout(), for example. 例如,您可以使用setTimeout()在Javascript端进行简单的轮询。 On the php side, you can just return the notification array even if it is empty (check it using Javascript) without any sleep(). 在php端,即使它为空,也可以只返回通知数组(使用Javascript检查),而不需要任何sleep()。 This should at least reduce the response time of your ajax request and maybe the loading time of pages. 这至少应减少ajax请求的响应时间,并可能减少页面的加载时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM