Pubnub: how to count the number of history chat messages before calling ngHistory

I want to know if we can restore the number of history messages in the pubnub channel before calling ngHistory. I am using Angularjs.

I need to show the bootloader until the message history is loaded. Now, since pubnub routes all the history and chat typed for one event, I need to hide the bootloader inside the event. But this creates a problem when there are no messages in the history. The bootloader will not be hidden because the event is not raised at least once. If I put the code to hide the counter outside the event, it will immediately hide the bootloader long before the history is loaded. I tried using the callback for ngHistory, but it was not running.

If I could get the number of messages, I can hide the bootloader if the number of history messages is 0.

I am using the code below:

$scope.limit = 50;
ActivityIndicator.showSpinner(); // to show a spinner

PubNub.ngHistory({
                    channel : $scope.channel,
                    limit   : $scope.limit
                    });

$rootScope.$on(PubNub.ngMsgEv($scope.channel), function(ngEvent, payload) {

            $scope.messages.push(payload.message);
            ActivityIndicator.hideSpinner(); // to hide the spinner

        }); 

The problem is that there are no messages, the event does not fire, if the event does not fire, the hide counter will never be called.

By default in pubnub they are implemented as follows. If they can guarantee that the event will be raised at least once, then the problem will be solved. I looked into the pubnub Angularjs library, I could find out the following codes of message paths to the event:

c.ngHistory = function(args) {
        args.callback = c._ngFireMessages(args.channel); // to route messages to event
        return c.jsapi.history(args);
      };

If I comment on the second line and implement a callback to call ngHistory as follows:

PubNub.ngHistory({
                    channel : $scope.channel,
                    limit   : $scope.limit,
                    callback : function(payload){
                        $scope.messages.push(payload.message);
                       ActivityIndicator.hideSpinner(); // to hide the spinner
                            }

                    });

Then the code works fine. But since editing the library file is not recommended. I should not do that. Do you have another idea to fix this problem?

+4
1

AngularJS PubNub SDK , ngHistory()

. limit limit : 10 PubNub.ngHistory({ ... }).

+2

All Articles