php - 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.
解决方案
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.
推荐阅读
- presto - 在 presto 中计算偏度和峰度
- lua - Add to table only 10 values
- php - wp_tag_cloud 在简单页面上显示结果
- gradle - 多模块项目中的gradle继承
- android - 打开预先存在的 github 库时,Android 上的 CMAKE 和 NDK 出错。(使用 Windows 10)
- javascript - 从连续数字生成数字序列
- javascript - 在 React 中切换组件
- perl - Perl:将(Log4perl-)对象传递给模块
- go - 检查结构字段是否为空
- python - 如何从文本文件中随机删除 100 个块