Giter Site home page Giter Site logo

Comments (4)

czechboy0 avatar czechboy0 commented on June 20, 2024 1

Hi @miguel-arrf,

any sort of live pub/sub system should be a good match for any of the three streaming formats we added built-in support for recently: JSON Lines, JSON Sequence, or Server-sent Events. Unless you need something more advanced, I think starting with JSON Lines is a good idea, as it's the simplest one.

Note that what's provided is just a way to produce/parse a stream of events, but as you point out, any logic to manage the connection and resumes, for example for a client that needs to handle such a stream in a HTTP response, would need to be written by you, wrapping the low-level generated calls to the server.

The idea would be, each event has a unique ID, you keep track of the latest ID you saw, and if the response stream gets interrupted, make the HTTP request again, sending the latest ID to the server, allowing the server to only replay what you missed since that ID.

So to answer your question: yes, the provided tools can be used as the low-level building blocks, but the stateful connection management needs to be built on top of it, as the details can differ between applications.

from swift-openapi-generator.

czechboy0 avatar czechboy0 commented on June 20, 2024 1

Great question, it's not immediately obvious.

For example, if you make a request to GET /stream and get back 200 OK immediately, with a content type application/jsonl, you'd get the HTTPBody type, which represents the response body stream.

As shown in the examples, you'd then turn it into a stream of parsed, deserialized events, so the full thing looks like this:

let response = try await client.getGreetingsStream(
    query: .init(name: "Example", count: 3),
    headers: .init(accept: [.init(contentType: .application_jsonl)])
)
let greetingStream = try response.ok.body.application_jsonl.asDecodedJSONLines(
    of: Components.Schemas.Greeting.self
)
for try await greeting in greetingStream { 
    print("Got greeting: \(greeting.message)") 
}

Now, if the connection gets interrupted, the for loop at the end is what I'd expect to throw an error. But the details depend on the ClientTransport implementation you're using.

But if you wrap the for loop in a do/catch block, that at least signals to you when you need to re-establish the connection.

from swift-openapi-generator.

miguel-arrf avatar miguel-arrf commented on June 20, 2024

Hi @czechboy0!

Thank you so much for your detailed answer 🚀 😊 ! Just one more question:

if the response stream gets interrupted

How do I know if the stream got interrupted?

(I haven't yet run the example code as I'm still understanding my options. Maybe if I have had run it I would already have the answer for this 😓 )

from swift-openapi-generator.

miguel-arrf avatar miguel-arrf commented on June 20, 2024

Awesome, thank you so much for all the help!!

from swift-openapi-generator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.