stream method

Stream<LedgerResponse> stream ()

Allows to stream SSE events from horizon. Certain endpoints in Horizon can be called in streaming mode using Server-Sent Events. This mode will keep the connection to horizon open and horizon will continue to return responses as ledgers close. See: Server-Sent Events

Implementation

//  See: <a href="https://www.stellar.org/developers/horizon/learn/responses.html" target="_blank">Response Format documentation</a>
Stream<LedgerResponse> stream() {
  StreamController<LedgerResponse> listener =
      new StreamController.broadcast();
  EventSource.connect(this.buildUri()).then((eventSource) {
    eventSource.listen((Event event) {
      if (event.data == "\"hello\"" || event.event == "close") {
        return null;
      }
      LedgerResponse ledgerResponse =
          LedgerResponse.fromJson(json.decode(event.data));
      listener.add(ledgerResponse);
    });
  });
  return listener.stream;
}