PubNub How to handle failed requests

239 Views Asked by At

I'm using Pubnub JS and need to handle subscribe request fails like 400 403 etc

addListener method don't have such option to handle error

1

There are 1 best solutions below

0
Darryn Campbell On BEST ANSWER

You want to listen for status events and filter by category, i.e.:

pubnub.addListener({
  status: function (s) {
    const affectedChannelGroups = s.affectedChannelGroups; // Array of channel groups affected in the operation
    const affectedChannels = s.affectedChannels; // Array of channels affected in the operation
    const category = s.category; // Returns category (status event)
    const operation = s.operation; // Returns PNSubscribeOperation
    const lastTimetoken = s.lastTimetoken; 
    const currentTimetoken = s.currentTimetoken; 
    const subscribedChannels = s.subscribedChannels; 
  },
});

For a list of categories, see https://www.pubnub.com/docs/sdks/javascript/api-reference/configuration#listener-status-events. I'm not sure how they map to the specific 4xx error codes but there is a dedicated PNUnknownCategory for non-200 responses.