Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel CRON or Event process respond to api request via long poll - how to re-vitalise the session

Tags:

php

redis

laravel

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.

like image 286
brianlmerritt Avatar asked Oct 23 '18 13:10

brianlmerritt


1 Answers

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.

like image 141
Patrick Cunningham Avatar answered Oct 10 '22 19:10

Patrick Cunningham