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
Support for timeline streaming via server-sent events #103
Comments
Thanks for reporting this @bensyverson , that's a very cool insight. Indeed, the streaming consists of facilitating the loading of different timelines. The |
Hey @kkostov, @davidgarywood I'm planning to use streaming endpoint to observe notifications https://docs.joinmastodon.org/methods/streaming/#notification and was wondering on how to add this to the sdk. Did you think about this topic previously and have any ideas on how it could be integrated. Should it be added to existing streams? Or something different? |
I had planned to come back and integrate the mastodon streaming api under our data streams one, but hadn’t thought too deeply about it as it was in our kinda post 1.0 list. Well, we’re post 1.0 now 😃 I think, I’d love to see a draft PR with a new object, separate from the existing streams, and to then take a step back and see if they can be integrated easily or not. It’ll either obviously slot in, or it’ll clearly need to be separate - but that shouldn’t be anyone’s concern when first pulling it together ! |
@Tunous are you using the current streams implementation in production? |
@kkostov No, my app deals with timelines in non-standard way and the current streams implementation didn't work for my needs. I'm using TootClient.getTimeline(_:pageInfo:limit:) directly. |
Thanks @Tunous, I think Dave's summary is spot on as well. We could try to fit the implementation as an extension to Streams as we have several useful concepts that overlap.
Mastodon offers two ways to subscribe to events, HTTP and WebSockets. I believe WebSockets is the one supported by most flavours, and this may require some research on how to best implement so we can support all target platforms (I'm mostly concerned about Linux and watchOS/tvOS/visionOS). I quickly searched and found for example https://github.com/vapor/websocket-kit or https://github.com/launchdarkly/swift-eventsource (if their license is compatible). Perhaps it will be worth adding an abstraction on top of that layer so later we can add the second protocol as a fallback. |
For every TootServerEvent<TootServerUpdate> {
// the timeline which produced this event
let timeline: Timeline
let update: TootServerUpdate
} Here for await event in try await client.data.stream([Timeline.local, Timeline.notifications, ...]) {
switch event.timeline {
case .local: ...
case .notifications: ...
case ...
}
}
How do you feel about this @Tunous @davidgarywood? |
@kkostov API like this looks good to me, especially as an async stream. There is one thing worth considering which could affect the implementation. What would happen if someone activated multiple streams at the same time. For example to handle home timeline events and different one for notifications. These could be called from different parts of the app. SDK could activate different connections per stream but I believe a better option would be to have one connection per TootClient. The implementation should be able to register multiple streams, deliver events to correct one and disconnect only when all streams finish. |
@Tunous that's a good question indeed and I totally agree with your suggestion - a single TootClient instance should maintain a single connection per stream/category (https://docs.joinmastodon.org/methods/streaming/#streams).
|
Hi all, based on the names of some of the API methods, I mistakenly thought TootSDK already supported Mastodon's streaming API:
So I was super confused when I used TootSDK and it did not stream new posts. Digging into the code, it looks like "stream" refers to a Swift
AsyncStream
. And rather than calling the streaming endpoint for the user's home timeline (GET /api/v1/streaming/use
), TootSDK calls the single-use endpoint (GET /api/v1/timelines/home
).For my purposes it's fine to periodically call
.refresh()
, but at some point it would be amazing to have a real async stream for a long-lived HTTP request or websocket.Thanks for pulling together this SDK—it's nice!
The text was updated successfully, but these errors were encountered: