Search code examples
swiftsiesta-swift

Does Siesta support HTTP long polling


I have an API endpoint which responds with an endless sequence of JSON objects. Like

GET /commands?since=112233 {"cmd": "mycommand", "params": [...]} {"cmd": "othercommand", "params": [...]} ... The session almost never ends (the server has a setting to time it out eventually, but the typical value is hundreds thousand seconds).

Is there a way to use such an API with Siesta?


Solution

  • No, Siesta doesn’t directly support this. A fundamental assumption of Siesta is that URLs are resources that have a (finite) state, and you want to observe changes to that state over time. Siesta thus expects a fully materialized response before it actually starts parsing it & publishes the result. What you’re describing is a different use of HTTP that just doesn’t fit Siesta’s model.

    Now if that sequence of JSON objects you’re getting could reasonably be interpreted to represent the sequence of successive states of a resource as it changes over time, then it would be possible to make your own networking call outside of Siesta, then use Resource.overrideLocalData(…) to publish the updates as they arrive.

    Looking at the sample data you gave, however, this looks more like a sequence of actions or events than a sequence of states. Siesta’s resource-centric view of the world might not be a good fit for that. Siesta gives you the most benefit if the question “What is the current state of thing X?” is a meaningful and natural one for your API.