-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Please add a small example how to use this. #75
Comments
👋🏼 Hi @houmie I'm sorry you're having trouble getting started. I think the below sample should be close to what you need based on your code sample. I hope this helps! import Foundation
import LDSwiftEventSource
class MyEventHandler: EventHandler {
func onOpened() {
print("** onOpened")
}
func onClosed() {
print("** onClosed")
}
func onMessage(eventType: String, messageEvent: LDSwiftEventSource.MessageEvent) {
print("** onMessage received event type \(eventType) and event \(String(describing: messageEvent))")
}
func onComment(comment: String) {
print("** onComment with comment \(comment)")
}
func onError(error: any Error) {
print("** onError with error \(String(describing: error))")
}
}
let body: [String: Any] = [
"max_new_tokens": 256,
"max_tokens": 256,
"stream": true,
]
let json = try? JSONSerialization.data(withJSONObject: body, options: [])
guard let json = json else { exit(1) }
guard let url = URL(string: "your-url") else { exit(1) }
var eventHandler = MyEventHandler()
var config = EventSource.Config(handler: eventHandler, url: url)
config.method = "POST"
config.headers = ["Content-Type": "application/json"]
config.body = json;
var eventSource = EventSource(config: config)
eventSource.start()
var semaphore = DispatchSemaphore(value: 0)
semaphore.wait() |
Hello @keelerm84 Thank you so much for this example. It was incredibly helpful. Yes it works. I have noticed without the semaphore, there is no guarantee that it would be working.
I have spent hours attempting to replace semaphores with different asynchronous patterns in SwiftUI, but without success. My struggles may stem from a lack of experience with Server-Sent Events (SSE). I'm considering whether to retain the semaphores and instead invoke eventSource.stop() at a specific moment. However, without knowing when the streaming will conclude, determining the perfect timing poses a challenge. I hope you can offer some advice on how to address this issue. |
Yes. This is because the main thread would exit otherwise. The event process work is all done asynchronously so you have to keep the application alive some other way.
I'm not sure I follow. If you have an application that is launching and you can instantiate the event source independent of its lifecycle, you shouldn't have a problem with the events coming in. As long as you retain a reference to the event source instance of course. Otherwise, it is going to shut everything down.
Under what conditions do you want to stop the event source? If the SSE server disconnects? You could just call |
Hello Mathew, Sorry for the late reply. I finally managed to replace the I only noticed one thing in the console when running it.
|
@houmie I'm not sure how I missed this original response. I realize it is very late, but just for completeness and for anyone else's future reference.
This library is built specifically to serve the needs of the LD SDKs. We expect the SSE connection to always be active. If it needs to be shutdown, that is for the consumer to decide, not this library.
Yes, this library implements some basic backoff and jitter retry logic. If a re-connect fails, the delay will increase up to some defined maximum. |
Hello,
Thank you first for this amazing project. I'm desperately looking for a SSE library in Swift.
I have difficulties understanding how to utilise this library in Swift. There is an API doc, but still very difficult to understand where to get started. A simple example would be amazing.
For example if I had this POST that requires Streaming as response, how would I use your library please?
Thanks
The text was updated successfully, but these errors were encountered: