1

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;
  }
}
4
  • Your final await loop can be replaced by yield* controller.stream Commented Jan 5 at 19:20
  • i did but the provider is always on the loading state , why ? Commented Jan 5 at 19:53
  • can we continue this in a discussion ? Commented Jan 5 at 19:55
  • everything related to the stream controller won't work , the yield and even when i add data to it Commented Jan 5 at 20:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.