Soneso / stellar_flutter_sdk

Stellar SDK for flutter - dart, Stellar, Horizon, Soneso
MIT License
71 stars 33 forks source link

Payments stream not stable #64

Closed jopmiddelkamp closed 11 months ago

jopmiddelkamp commented 12 months ago

The payment stream only works for a short amount of time.. If I open the stream and I execute 4 payments in a row one after another it shows all the 4 transactions. But if I wait for a couple of minutes the executable of the code example below just crashes/stops working.

When listening to the stream via laboratory and the JavaScript SDK everything works fine. They both keep the connection open for as long as I wish tried 10 minutes even and it was no problem the transactions still showed up. Only somehow the Dart stream seems to crash or quit..

Dart code (not working when starting, waiting a couple of minutes and then execute a transaction):

Future<void> main() async {
  // Create a server instance
  StellarSDK sdk = StellarSDK.TESTNET;

  final completer = Completer<void>();

  // Listen to the stream for account changes
  sdk.payments
      .forAccount('GA7S6R5RWAQUIWESLLLZC23IFENBB6BBGUSJGFBWXFSZIB5MXEWZ6CP3')
      .cursor('1350672725331969')
      .order(RequestBuilderOrder.ASC)
      .stream()
      .doOnCancel(() {
    print("Stream cancelled");
    completer.complete();
  }).doOnError((error, stackTrace) {
    print("Error in stream: $error");
  }).listen(
    (operationResponse) {
      print('');
      print("##################################");
      print("### ${operationResponse.transactionHash} received ###");
      print("##################################");
    },
    onError: (error) {
      print("Error in stream: $error");
    },
  );

  await completer.future;
}

JavaScript (working fine even after its been idle for 10 minutes+):


var StellarSdk = require('stellar-sdk');
var server = new StellarSdk.Server('https://horizon-testnet.stellar.org');

var accountHandler = function (operationResponse) {
    console.log("");
    console.log("##################################");
    console.log("### " + operationResponse.transaction_hash + " received ###");
    console.log("##################################");
    console.log("");
};
var accountId = "GA7S6R5RWAQUIWESLLLZC23IFENBB6BBGUSJGFBWXFSZIB5MXEWZ6CP3";
var es = server.payments().forAccount(accountId).cursor('1350672725331969').order("asc").stream({
    onmessage: accountHandler
});

console.log("Stream started");
christian-rogobete commented 12 months ago

Hi @jopmiddelkamp, thank you for reporting this issue. I guess your code is running on a mobile phone within an app? Is the app still open?

jopmiddelkamp commented 12 months ago

First came across this on my mobile phone as my code didn't work as intended.. Found out the stream somehow only worked for a couple of minutes.. Then to be sure I setup the example that you can see above in a Dart executable that I run on my MacBook. Same result only in my Flutter app it did not crash the app but it seems to crash the Dart executable. But only if the connection is idle for a while, if I push a couple of payments directly after the application runs it shows them correctly but then when I wait a couple of minutes and then push a payment then it seems to crash.

jopmiddelkamp commented 12 months ago

stellar_flutter_sdk_poc.zip

christian-rogobete commented 12 months ago

ok, I will check it with a test case. I asked because, as I know, if the app goes to the background streaming dose not work any more. In this case one must listen on the server side and send a push notification to the app.

christian-rogobete commented 12 months ago

I guess that the server closes the stream.

Following payments using curl:

$ curl -H "Accept: text/event-stream" "https://horizon-testnet.stellar.org/accounts/GAVMXA2LAJWG5ZXXABCVXO5KZ3HU23C3EE3XO2RJ7SPAHSN5JLIWFXUV/payments"

see: https://developers.stellar.org/docs/tutorials/follow-received-payments

Screenshot 2023-07-03 at 16 06 50

christian-rogobete commented 12 months ago

after 1 minute

jopmiddelkamp commented 12 months ago

Yes I know, pushing notifications is still on our list of TODO's but so far have not gotten to that point yet. Not too far out anymore :) I think I'll write a temp workaround for this till we implement the push notifications.

But makes me wonder why is the JavaScript SDK version not closing after one minute..

christian-rogobete commented 11 months ago

fixed in release 1.5.7