subscribe method

Future<Stream<OracleUcoPrice>> subscribe()

Implementation

Future<Stream<OracleUcoPrice>> subscribe() async {
  final phoenixHttpLink = HttpLink(
    phoenixHttpEndpoint,
  );

  final phoenixLink = await PhoenixLink.fromWebsocketUri(
    uri: websocketEndpoint,
  );

  final link = Link.split(
    (request) => request.isSubscription,
    phoenixLink,
    phoenixHttpLink,
  );
  final client = GraphQLClient(
    link: link,
    cache: GraphQLCache(),
  );

  final subscriptionDocument = gql(
    'subscription { oracleUpdate { timestamp, services { uco { eur, usd } } } }',
  );
  return client
      .subscribe(
        SubscriptionOptions(
          document: subscriptionDocument,
        ),
      )
      .map((result) => _oracleUcoPriceDtoToModel(data: result.data))
      .where((valueOrNull) => valueOrNull != null)
      .cast();
}