Riverpod stream provider with SignalR

50 Views Asked by At

iam trying to implement signalr with riverpod using a stream and this stream will return the live feed parameter , i have tried to make a future provider for the connection and a stream provider for returning the value with hubconnection.on in a separate Provider but it didn't work , the connection starts and the data is received but is not handled by the streamcontroller and the provider stuck in the loading state . here is what got so far but it has the same issue :

@riverpod
Stream<Object?> signalRStream(Ref ref) async* {
  Logger.root.level = Level.ALL;
// Writes the log messages to the console
  Logger.root.onRecord.listen((LogRecord rec) {
    print('${rec.level.name}: ${rec.time}: ${rec.message}');
  });
  final token = await ref.read(settingsNotifierProvider.notifier).getToken();

  const String url = 'xxxxxxxxxxxxxx.xxx';
  final hubProtLogger = Logger("SignalR - hub");
  final hubConnection = HubConnectionBuilder()
      .withUrl(url,
          options: HttpConnectionOptions(
              skipNegotiation: true, // Skip negotiation step for WebSocket
              logger: hubProtLogger,
              transport: HttpTransportType.WebSockets,
              requestTimeout: 50000,
              accessTokenFactory: () async => token!))
      .build();

  await hubConnection.start();

  print("SignalR connection started");

  print("Sending message");
  await hubConnection.send("JoinLiveFeed", args: [
    [37, 38]
  ]);

  var result = await hubConnection.invoke("JoinLiveFeed", args: <Object>[
    [37, 38]
  ]);
  final StreamController controller = StreamController<List<Object>>();
  hubConnection.on("SendSymbolsListAsync", (parameters) {
    controller.add(parameters ?? []);
  });
  await for (final value in controller.stream) {
    yield value;
  }
}
0

There are 0 best solutions below