pubnub / pubnub-angular2

Other
32 stars 13 forks source link

Issue when reusing default instance #37

Open andrewryan1906 opened 4 years ago

andrewryan1906 commented 4 years ago

I'm using PubNub in conjunction with AWS Step Functions to observe a step function during execution. I have two step functions; Step Function A and Step Function B.

When I run function A, everything works OK; I get all of the PubNub communications. However, when I run function B, I get nothing. This is the code I'm using inside of my step function poling service:

  constructor(private stepFunctionsService: StateMachinesService, private securityService: SecurityService, private pubNub: PubNubAngular) {
  }

  WaitForStepFunctionExecutionToComplete<T>(token: StepFunctionExecutionToken, progressSubject$?: Subject<StepFunctionProgressMessage>): Observable<T> {

    if (token.notification_channel && progressSubject$) {
      console.log('Initializing PubNub');
      this.pubNub.init({
        publishKey: token.notification_publishKey,
        subscribeKey: token.notification_subscribeKey
      });

      this.pubNub.subscribe({
        channels: [token.notification_channel],
        triggerEvents: ['message']
      });
      this.pubNub.getMessage(token.notification_channel, msg => {

        return progressSubject$.next(msg.message);
      });

    }

So, once I kick off a step function, I run the pubNub.init() method. The first time, it works fine; the second time, it won't reset/reinitialize the instance.

What am I doing wrong? How can I get the default pubNub instance to "reset," so I can use it with other channels/instances?

colonelpopcorn commented 4 years ago

Per the documentation, you are not really supposed to deinitialize or create a new pubnub instance. You should probably just use separate channels and a singleton service.

https://www.pubnub.com/docs/angular2-javascript/pubnub-javascript-sdk#inject-pubnub-angular2 https://angular.io/guide/singleton-services