首页 > 解决方案 > Laravel CRON or Event process respond to api request via long poll - how to re-vitalise the session

问题描述

I have a poll route on an API on Laravel 5.7 server, where the api user can request any information since the last poll.

The easy part is to respond immediately to a valid request if there is new information return $this->prepareResult($newData);

If there is no new data I am storing a poll request in the database, and a cron utility can then check once a minute for all poll requests and respond to any polls where data has been updated. Alternatively I can create an event listener for data updates and fire off a response to the poll when the data is updated.

I'm stuck with how to restore each session to match the device waiting for the update. I can store or pass the session ID but how do I make sure the CRON task / event processor can respond to the correct IP address just as if it was to the original request. Can php even do this?

I am trying to avoid websockets as will have lots of devices but with limited updates / interactions.

标签: phplaravelredis

解决方案


Clients poll for updates, APIs do not push updates.

REST API's are supposed to be stateless, so trying to have the backend keep track goes against REST.
To answer your question specifically, if you do not want to use websockets, the client app is going to have to continue to poll the endpoint till data is available.


推荐阅读