-
-
Notifications
You must be signed in to change notification settings - Fork 51
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 server-sent events #664
Comments
Maybe this is a good opportunity to rework the streaming API to be an async iterator? I can imagine something like:
Question what happens when the connection drops:
(If you need code that consumes SSE with |
@assaf I'd also like to move the event interfaces to Async Iterators. We've already had Async Iterators for pagination in our code base, so it'd be nice to integrate those APIs. We can also drop eventemitter3 from our dependency. (Just note that, If we move on to the Async Iterator, I'd like to change all the event interfaces to that one, so not only SSE but also WebSocket)
I don't know what you mean by drop, but the Web API
I'd like to see how it's like. Is there some advantage to implementing by |
I don’t think it’s necessary to do both SSE and WS. It’s the same mechanism on the server, SSE just seems simpler to me. I generally avoid If the connection breaks, If the server drops the connection — doesn't tell the client it's closed — then the only way the client can detect is with a timeout. Mastodon sends a heartbeat every 1 second, so it's possible to add code that detects timeout within a few seconds. |
You're right but it'd be better if we could provide an option that users can choose which implementation to use. Even though the event payloads are completely the same, WS and SSE have a bit different ways of establishing the connection. In WS we can create multiple subscriptions through a single connection: https://docs.joinmastodon.org/methods/streaming/#websocket
This sounds critical because the doc says streaming a home timeline and notifications requires the
I still didn't completely figure out how is it like to implement EventSource from scratch by I'd like to see your PoC anyways 👍 |
This is the code I use for reading the public timeline:
So says the docs, but the server does accept
While SSE has this feature ( The workaround is when the stream fails, you catch up by reading from the server, eg. It would look something like:
|
@assaf Thank you for sharing! It’d help a lot to implement it.
(This is totally off-topic but) Inherently HTTPS doesn’t encrypt request URLs, so attackers can exploit access tokens by putting a honeypot in. This is one of the biggest issues of WebSocket so Mastodon allows us to use Avoiding using
Your snippet above is a good idea, but I'm concerned that your technique can’t be applied to events other than a new status publication. There are events that you’ll never be able to fetch later if you missed the first delivery, such as I thought it’s worth considering simply throwing an error in event of a connection drop and delegating the catch-ups to the users, if catch-ups are incomplete and ended up leading to unnecessary confusion. It looks not that difficult to get a catch-up event if users really want to get lost events even if it's not supported by Masto.js, as you described in the snippet above. |
That's what I'm proposing, and how the code snippet works. As soon as it detects a problem with the stream, it bails by throwing an exception. The streaming API does not attempt to recover from errors. The application then has to deal with the error as best as it can, which is indeed limited.
FYI HTTPS is essentially HTTP over TLS over TCP, so everything in the HTTP request — The only piece of information that's not encrypted is the hostname. TLS exposes that as Subject Alternative Name (SAN) so load balancers can route requests to the right server. There's a different reason to avoid access tokens in URLs: the request path is often logged including query string and without filtering. |
Okay now I understand and agree with your proposal
Ah... I've heard there are some drawbacks to using query parameters as a way of authorisation and previously I've seen HTTP debugger Charles can detect HTTPS requests with hostname even if I don't modify certificates, so I confused everything |
We need the option to choose which implementation should be used as a streaming, server-sent events or WS
https://docs.joinmastodon.org/methods/timelines/streaming/#server-sent-events-http
The text was updated successfully, but these errors were encountered: