public function action_messages() { $receivedTotal = (int) $this->request->query('total'); $model = new Model_Message(); $currentTotal = $model->countMessages(); while ($receivedTotal == $currentTotal) { $currentTotal = $model->countMessages(); sleep(1); } $this->response->headers('Content-Type', 'application/json; charset=utf-8'); $messages = $model->getLastMessages(25); return $this->response->headers('Content-Type', 'application/json; charset=utf-8')->body(json_encode(['received' => $receivedTotal, 'messages' => $messages, 'total' => $currentTotal])); }