konsultaner / connectanum-dart

This is a WAMP client (Web Application Messaging Protocol) implementation for the dart language and flutter projects.
MIT License
22 stars 14 forks source link

Strange behaviour of Session.publish and/or Session.subscribe #41

Closed aarlt closed 2 years ago

aarlt commented 2 years ago

It looks like that there is something strange with Session.publish and/or Session.subscribe. It might also be possible that something is off in my router implementation. Sorry, if this is the case.

The following program

  final subscription = await session1.subscribe(
      'topic');
  subscription.eventStream!.listen(
      (event) => print('received from <id>.push: \'${event.arguments![0]}\''));
  // ...
  print('before publish: 1');
  await session2.publish(
      'topic',
      arguments: ['1'], options: PublishOptions(acknowledge: true));
  print('after publish: 1');

  print('before publish: 2');
  await session2.publish(
      'topic',
      arguments: ['2'], options: PublishOptions(acknowledge: true));
  print('after publish: 2');

  print('before publish: 3');
  await session2.publish(
      'topic',
      arguments: ['3'], options: PublishOptions(acknowledge: true));
  print('after publish: 3');

  print('before publish: 4');
  await session2.publish(
      'topic',
      arguments: ['4'], options: PublishOptions(acknowledge: true));
  print('after publish: 4');

is generating the following output

before publish: 1
after publish: 1
before publish: 2
received from <id>.push: '1'
after publish: 2
before publish: 3
received from <id>.push: '2'
after publish: 3
before publish: 4
received from <id>.push: '3'
after publish: 4

It looks like that somehow the first publish is not directly triggering the subscription handler. Also the last event seem to not trigger the subscriber. It looks somehow like that a new publish just triggers the reception of an older event.

Expected output

before publish: 1
received from <id>.push: '1'
after publish: 1
before publish: 2
received from <id>.push: '2'
after publish: 2
before publish: 3
received from <id>.push: '3'
after publish: 3
before publish: 4
received from <id>.push: '4'
after publish: 4

Any ideas?

konsultaner commented 2 years ago

@aarlt you are using crossbar which is much more mature then this packages. So I bet, the issue is on my side. I havn't found the issue right away. I'll fix this during the week I hope!. Pretty busy atm.

konsultaner commented 2 years ago

@aarlt I just took a closer look to your issue report. Actually this is absolutly expected:

before publish: 1
after publish: 1
before publish: 2
received from <id>.push: '1'
after publish: 2
before publish: 3
received from <id>.push: '2'
after publish: 3
before publish: 4
received from <id>.push: '3'
after publish: 4

The received from <id>.push: '3' is not awaited but totally async.

await session2.publish(
      'topic',
      arguments: ['4'], options: PublishOptions(acknowledge: true));

only receives a published, but not a has been receivced by the subscriber. So even if the await was satisfied, doesn't mean that the client has received the event. Returning the published is much faster then sending an event from a publish. There is almost no way that your expected result will happen. The only way to achieve this is to add a stream yourself, that is fed with the event you receive in your subscriber and await it.