Giter VIP home page Giter VIP logo

dart-eventsource's Introduction

eventsource

A library for using EventSource or Server-Sent Events (SSE). Both client and server functionality is provided.

This library implements the interface as described here.

Client usage

For more advanced usage, see the example/ directory. Creating a new EventSource client is as easy as a single call. The http package is used under the hood, so wherever this package works, this library will also work. Browser usage is slightly different.

EventSource eventSource = await EventSource.connect("http://example.com/events");
// in browsers, you need to pass a http.BrowserClient:
EventSource eventSource = await EventSource.connect("http://example.com/events", 
    client: new http.BrowserClient());

Server usage

We recommend using shelf_eventsource for serving Server-Sent Events. This library provides an EventSourcePublisher that manages subscriptions, channels, encoding. We refer to documentation in the shelf_eventsource package for more information.

This library also includes a server provider for dart:io's HttpServer in io_server.dart. However, it has some issues with data flushing that are yet to be resolved, so we recommend using shelf instead.

Licensing

This project is available under the MIT license, as can be found in the LICENSE file.

dart-eventsource's People

Contributors

b1rdg avatar dhruvdabhi101 avatar mehcode avatar robin7331 avatar samjakob avatar selankon avatar stevenroose avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

dart-eventsource's Issues

Can't authenticate request

Is it possible to add the possibility to add authorization header?
without it becomes useless in a secured environment :(
i've been looking all around last two days and it looks like this feature is missing in the original eventsource api, but there are some implementations with polyfill

POST not working

Hello,
I'm having problems using the post.
I have tried on web

 [...]

 Map body = {
        'text': message.text,
        'apikey': APIKEY,
      };

      EventSource eventSource = await EventSource.connect(
          "myserverurl.com", client: new BrowserClient(),
          body: jsonEncode(body));
      // listen for events
      eventSource.listen((Event event) async {

     [...]

My server is not receiving the post variables
Hopefully it can be fixed
Thanks :)

Bump dart version

Hi, thank you for this library!

Is it possible to bump the dart sdk version to use one of the 2.1.0? Like 2.1.0-dev.3.0.

Thanks!

handle authentication (flutter app)

Endpoint is secured with oauth2, therefore requires valid access token.

Any suggestions?

EventSource.connect("/api/conn/$username", {access_token_required});

Cannot close event source from client side

I receive an exception when trying to close an EventSource from the client side.

import 'package:eventsource/eventsource.dart';
...
EventSource sseClient;
...
sseClient = await EventSource.connect( <VALID_URL> ]);
...
sseClient.client.close();

Dart Error: Unhandled exception:
Connection closed while receiving data

Note: I could not find a close() method on EventSource shown in the non-normative developer note
of https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events.

Does not work on Web

Sadly this package does not work on web, as it currently stands. This is not directly to blame to the package author, but has to do with the fact that the http package semantically implies that a StreamedResponse also works on web, which it doesn't. It will always return after the complete response is received, which obviously, breaks this package. There is an ongoing discussion though on the http package here: dart-lang/http#595. There I posted an implementation of an http Client, that uses the fetch API to make the StreamedResponse work. You can pass it to EventSource.connect.

Additionally, I did not find any indication that this package does not work on web. You should indicate that in the README.

Co-Maintainer

Hi^^ I have seen you are currently not actively working/using this package anymore. Would you be interested in having a Co-Maintainer?

I could review PRs, do updates of packages to ensure compability with newer versions of dependencies and maybe even continue to develop this package further.

Dart 2 Error

I'm running Dart 2.0.0 on Mac, and trying to finish up https://github.com/angel-dart/eventsource.
However, I get an error on startup. I think this should be pretty easy to patch up; would you mind if I sent in a PR to correct it?

Here's the error:

(virtualenv_py3) wg-dhcp201d207d109:eventsource thosakwe$ dart example/main.dart
file:///Users/thosakwe/.pub-cache/hosted/pub.dartlang.org/eventsource-0.2.0/lib/publisher.dart:109:23: Error: The argument type 'eventsource.src.proxy_sink::ProxySink<dynamic>' can't be assigned to the parameter type 'dart.core::Sink<eventsource.src.event::Event>'.
Try changing the type of the parameter, or casting the argument to 'dart.core::Sink<eventsource.src.event::Event>'.
        _cache.replay(sub, lastEventId, channel);
                      ^

Please update logging dependency to be compatible with latest web libraries

I get this when I try to use your library at ^0.3.0. Logging is at 1.0.2 now.

Because build_web_compilers >=3.0.0 depends on logging ^1.0.0 and eventsource >=0.2.0 depends on logging ^0.11.0, build_web_compilers >=3.0.0 is incompatible with eventsource >=0.2.0. So, because blaseballstatus depends on both eventsource ^0.2.0 and build_web_compilers ^3.0.0, version solving failed.

Reconnect on Flutter Mobile App

When trying this via a Flutter Mobile App in Android Studio. It did not seem to reconnect when the server connection is stopped. It looks like the HttpClient doesn't do an error or done function when the connection terminates

Spell Mistake in Readme

For more advanced usage, see the example/ directory. Creating a new EventSource client is as easy as a single call. The http package is used under the hood, so wherever this package works, this lbirary will also work. Browser usage is slightly different.

Here spelling of library is wrong.

Dart 2 - Not a subtype error

I use Dart VM version: 2.1.0-dev.9.4.flutter-f9ebf21297

I think dart does not match type

Unhandled exception:
type '(Duration) => void' is not a subtype of type '(dynamic) => void'
#0 new EventSource._internal (package:eventsource/eventsource.dart:69:55)
#1 EventSource.connect (package:eventsource/eventsource.dart:63:26)

FormatException if EventSource does not contain valid utf8

Hello,

I have an external API that is returning me an EventStream with a JSON content, but it seems like they are returning an invalid utf8, since when connecting to it I have this error the majority of events.

Unhandled Exception: FormatException: Missing extension byte (at offset 7291)
E/flutter (12039): #0      _Utf8Decoder.convertChunked (dart:convert-patch/convert_patch.dart:1887:7)
E/flutter (12039): #1      _Utf8ConversionSink.addSlice (dart:convert/string_conversion.dart:314:28)
E/flutter (12039): #2      _Utf8ConversionSink.add (dart:convert/string_conversion.dart:310:5)
E/flutter (12039): #3      _ConverterStreamEventSink.add (dart:convert/chunked_conversion.dart:72:18)
E/flutter (12039): #4      _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:121:24)
E/flutter (12039): #5      _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter (12039): #6      _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter (12039): #7      _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter (12039): #8      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:357:11)
E/flutter (12039): #9      _BufferingStreamSubscription._add (dart:async/stream_impl.dart:285:7)
E/flutter (12039): #10     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:127:11)
E/flutter (12039): #11     _HandleErrorStream._handleData (dart:async/stream_pipe.dart:266:10)
E/flutter (12039): #12     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:157:13)
E/flutter (12039): #13     _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter (12039): #14     _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter (12039): #15     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter (12039): #16     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:357:11)
E/flutter (12039): #17     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:285:7)
E/flutter (12039): #18     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:127:11)
E/flutter (12039): #19     _HandleErrorStream._handleData (dart:async/stream_pipe.dart:266:10)
E/flutter (12039): #20     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:157:13)
E/flutter (12039): #21     _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter (12039): #22     _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter (12039): #23     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter (12039): #24     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:357:11)
E/flutter (12039): #25     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:285:7)
E/flutter (12039): #26     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:808:19)
E/flutter (12039): #27     _StreamController._add (dart:async/stream_controller.dart:682:7)
E/flutter (12039): #28     _StreamController.add (dart:async/stream_controller.dart:624:5)
E/flutter (12039): #29     _HttpParser._doParse (dart:_http/http_parser.dart:809:28)
E/flutter (12039): #30     _HttpParser._parse (dart:_http/http_parser.dart:324:7)
E/flutter (12039): #31     _HttpParser._onData (dart:_http/http_parser.dart:853:5)
E/flutter (12039): #32     _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter (12039): #33     _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter (12039): #34     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter (12039): #35     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:357:11)
E/flutter (12039): #36     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:285:7)
E/flutter (12039): #37     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:808:19)
E/flutter (12039): #38     _StreamController._add (dart:async/stream_controller.dart:682:7)
E/flutter (12039): #39     _StreamController.add (dart:async/stream_controller.dart:624:5)
E/flutter (12039): #40     _Socket._onData (dart:io-patch/socket_patch.dart:2044:41)
E/flutter (12039): #41     _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter (12039): #42     _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter (12039): #43     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter (12039): #44     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:357:11)
E/flutter (12039): #45     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:285:7)
E/flutter (12039): #46     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:808:19)
E/flutter (12039): #47     _StreamController._add (dart:async/stream_controller.dart:682:7)
E/flutter (12039): #48     _StreamController.add (dart:async/stream_controller.dart:624:5)
E/flutter (12039): #49     _RawSecureSocket._sendReadEvent (dart:io/secure_socket.dart:1002:19)
E/flutter (12039): #50     _rootRun (dart:async/zone.dart:1182:47)
E/flutter (12039): #51     _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (12039): #52     _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter (12039): #53     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter (12039): #54     _rootRun (dart

It would be great if there could be a way to allow invalid UTF8 characters in the Event.data.

Continuous trigger on the listeners

Hi all,
I'm using the package into an app running on iPhone.
I created the object as:

eventSource = await EventSource.connect("https://my.com/php/utility/notify.php");
      eventSource!.onOpen.listen((event) {
        debugPrint("SSE OPEN $event");
      });
      eventSource!.onMessage.listen((event) {
        debugPrint("SSE onMSG data:'${event.data}' ev:'${event.event}' id:${event.id}");
      });
      eventSource!.onError.listen((event) {
        debugPrint("SSE ERROR $event");
      });
      eventSource!.listen((event) {
        debugPrint("SSE EVT data:'${event.data}' ev:'${event.event}' id:${event.id}");
      });

In the console I see a continuous output of the following:

flutter: SSE EVT data:'' ev:'message' id:null
flutter: SSE onMSG data:'' ev:'message' id:null
flutter: SSE EVT data:'' ev:'message' id:null
flutter: SSE onMSG data:'' ev:'message' id:null
flutter: SSE EVT data:'' ev:'message' id:null
flutter: SSE onMSG data:'' ev:'message' id:null
flutter: SSE EVT data:'' ev:'message' id:null
flutter: SSE onMSG data:'' ev:'message' id:null
flutter: SSE EVT data:'' ev:'message' id:null
flutter: SSE onMSG data:'' ev:'message' id:null
flutter: SSE EVT data:'' ev:'message' id:null
flutter: SSE onMSG data:'' ev:'message' id:null
flutter: SSE EVT data:'' ev:'message' id:null
flutter: SSE onMSG data:'' ev:'message' id:null
flutter: SSE EVT data:'' ev:'message' id:null
flutter: SSE onMSG data:'' ev:'message' id:null
flutter: SSE EVT data:'' ev:'message' id:null
flutter: SSE onMSG data:'' ev:'message' id:null
flutter: SSE EVT data:'' ev:'message' id:null
flutter: SSE onMSG data:'' ev:'message' id:null
flutter: SSE EVT data:'' ev:'message' id:null
flutter: SSE onMSG data:'' ev:'message' id:null
flutter: SSE EVT data:'' ev:'message' id:null
flutter: SSE onMSG data:'' ev:'message' id:null
flutter: SSE EVT data:'' ev:'message' id:null
flutter: SSE onMSG data:'' ev:'message' id:null
flutter: SSE EVT data:'' ev:'message' id:null
flutter: SSE onMSG data:'' ev:'message' id:null
flutter: SSE EVT data:'' ev:'message' id:null
flutter: SSE onMSG data:'' ev:'message' id:null
flutter: SSE EVT data:'' ev:'message' id:null
flutter: SSE onMSG data:'' ev:'message' id:null
flutter: SSE EVT data:'' ev:'message' id:null
...

at the frequency of almost 1 a second.
Is it correct?

Thanks!

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.