L
L
Leopandro2019-05-20 18:04:05
Angular
Leopandro, 2019-05-20 18:04:05

Why doesn't long polling work?

I made the first script with long polling, there is a problem - after lp request, the server does not respond to other requests until it answers this one. How to fix?

this.http.request('get', this.url).subscribe((data) => {
      alert(JSON.parse(data));
    }, error => {
      alert(error);
    });

public static function listenNotify(Request $request) {
    ignore_user_abort(false);
    set_time_limit(10);
    $id = Auth::user()->id;
    $limit = 0;
    while($limit < self::$timeout) {
      if ($notify = UserNotification::where([
        ['user_id', '=', $id],
        ['type', '=', 1],
        ['notified', '=', 0]
      ])->get()) {
        if (count($notify) > 0) {
          return response()->json($notify, 200);
        }
      }
      $limit++;
      sleep(1);
    }
    return response()->json([
      'timeout' => true
    ], 200);
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Demian Smith, 2019-05-20
@search

This is because PHP locks the session on every request. This queues up other ajax requests. In order to release a locked session, you can execute
somewhere before while.
$request->session()->save()in the depths of the framework, it causes session_write_close()that it releases the session and gives way to other requests.
All this sausage with session file locks is needed so that several requests in a row do not pollute the session. If you want to write something to the session after calling $request->session()->save(), you may have to re-open the session. I don't know if laravel will do it for you or not. In your place, I advise you to make sure yourself, so that later you don’t dance with a tambourine.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question