voryx / thruway.js

RxJS WAMPv2 Client
MIT License
28 stars 13 forks source link

How to unsubscribe (code 34) from subscribed topic? #17

Closed BillyGeat closed 6 years ago

BillyGeat commented 6 years ago

Hello,

subscribed successfully to a topic and getting messages from server into the angular app.

Now, after "successfully" unsubscribing via the "takeUntil" trick in Angular, no more messages arrive in the App, but the server still sends the subscribed topic. There is / was no code "34" for unsubscribe send to the server by thruway.js.

The code looks like this: ("Angular unsubscribe" from here: https://stackoverflow.com/questions/38008334/angular-rxjs-when-should-i-unsubscribe-from-subscription/41177163#41177163)

  private unsubscribe: Subject<void> = new Subject<void>()
  plan = [];

  planasync$ = this.wampService
    .topic('sendplan')
    .takeUntil(this.unsubscribe)
    .subscribe(
      res => { 
        let tm = new TableMap();
        this.plan = tm.tableToMap(res.argskw).filter((m) => m.area === 1);
      },
      err => console.log("res err", err),
      () => console.log("res complete"));

  constructor(private wampService: WampService) { }

  ngOnDestroy() {
    this.unsubscribe.next();
    this.unsubscribe.complete();
  }

How to send an "unsubscribe" also to the server? Am I missing something?

davidwdan commented 6 years ago

What version of thruway.js are you using? If it's not sending back an unsubscribe message, then it's most likely a bug.

BillyGeat commented 6 years ago

A npm list gives "thruway.js@1.3.0-beta5". I think, this should be the last / newest one, right?

davidwdan commented 6 years ago

I'm able to confirm that this is a bug. I'll have a fix for it later today.

BillyGeat commented 6 years ago

I just updated thruway.js to 1.3.0-beta6 and it works as expected! Thank you for the quick fix!

I saw in the changed files, you mainly added a delay of 100ms before sending the close socket commands. These 100ms should be enough for near every situation? Why are they needed at all?

davidwdan commented 6 years ago

When the topic is disposed (with takeUntil), it automatically closes the websocket connection if there are 0 subscribers. I added the delay to ensure that the connection is held open long enough to send messages that originate within a dispose, since they happen "out of band".