-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
How do we listen to Server Sent Event(SSE) Stream using Dio package #1279
Comments
I don't think dio allows for this use case at the moment |
That's very unfortunate . It's an important feature. As many apps use sse events now a days. e.g. stock broking apps etc. So if dio can add the feature it will be helpful. |
Do you have a free example api which offers this kind of stream? |
I don't have a free example. I'm testing on my servers stream |
I agree with it. SSE is a very important and popular feature, haha. |
You can use Socket.IO for server-side events for now. |
Yes, I'm using http package itself for now. As it gives expected result. |
You can use SSE with Response<ResponseBody> rs = await Dio().get<ResponseBody>(
"https://server/stream",
options: Options(headers: {
"Authorization":
'vhdrjb token"',
"Accept": "text/event-stream",
"Cache-Control": "no-cache",
}, responseType: ResponseType.stream), // set responseType to `stream`
); it gets result as StreamTransformer<Uint8List, List<int>> unit8Transformer =
StreamTransformer.fromHandlers(
handleData: (data, sink) {
sink.add(List<int>.from(data));
},
); and then you can transform it to json : rs.data?.stream
.transform(unit8Transformer)
.transform(const Utf8Decoder())
.transform(const LineSplitter())
.listen((event) {
log(event);
}); This worked for me and now i'm getting data from server |
Wow, this would be amazing to have built into Dio. The other solutions to this problem in Dart don't handle errors well, like retrying disconnections. For testing, there's a free SSE endpoint at https://hacker-news.firebaseio.com/v0/topstories.json. the documentation is at https://github.com/HackerNews/API |
Yup that will be great we need to include all the functionality that eventsource package provides for js.
|
SSE messages must be split by a double new line. Does anyone implement such a splitter? |
Based on @vhdrjb's code, I have implemented one more transformer to extract the correct SSE message. But I believe there must be a more efficient transformer. But with my current dart/flutter knowledge, that is what I can do.
|
Can you share how to disconnect the stream connection? |
I am on flutter web and when I use this code, the stream only starts when the whole response was recieved. I get all events, but I would like to receive them live. Is there some missing support for Web, do I need configure something or is this expected? |
The stream reading based on the XHR is not available. As of workaround, see dart-lang/http#595. |
Issue Info
Issue Description
I was trying to subscribe to server sent event(SSE) stream. I was able to connect to the stream using this code which uses HTTP package. It works totally as expected and returns steam of data. But generally I use Dio package. Hence I wanted to implement the same using dio flutter. But unfortunately I'm unable to do that.
This is what I tried with Dio package. But this returns 404 request not found. Mostly I'm not able to figure out how do I get a StreamedResponse using dio package.
I did search for example implementing SSE streams using dio but, I'm unable to find it.
The text was updated successfully, but these errors were encountered: