angularpubnub

Angular 10 + PubNub - Problem reading messages from a specific channel


I'm trying to use PubNub to read messages published in a specific channel. Publishing is working correctly.

On my Angular 10 Frontend I'm trying the following:

this.pubnubService.subscribe({ channels: ['channel1'], triggerEvents: true });

this.pubnubService.getMessage(this.currentlySubscribedPubNubChannels, message => {
  console.log(message);
});

However, no message is received when publishing (via my API or the Debug console on pubnub.com).

On the other side, if I specify a listener, I can receive messages on ALL channels (which is not what I want to achieve):

this.pubnubService.addListener({
  message: (message) => {
    console.log(message);
  }
});

Can anybody tell me why the general listener is working but listening on a particular channel is not?

Any help is appreciated, thanks, Pascal


Solution

  • It seems that the package: https://www.npmjs.com/package/pubnub-angular2 is no longer supported.

    Using the "pubnub" (https://www.npmjs.com/package/pubnub) package in combination with: https://www.npmjs.com/package/@types/pubnub

    seems to do the trick.

    import * as Pubnub from 'pubnub';
    ...
    export class MyApp {
    
      private pubnubService;
    
      constructor() {
        this.pubnubService = new Pubnub({
          subscribeKey : '<SUBSCRIBER KEY>'
        });
    
        this.pubnubService.subscribe({
          channels: [<YOUR CHANNEL>]
        });
    
        this.pubnubService.addListener({
          message: ((msg) => {
            // handle event
          }).bind(this)
        });
      }
    }