Pubnub chat in Angular

Viewed 529

I'm implementing the pubnub chat in my AngularJS application. I'm following this tutorial

The problem is that if I create a new AngularJS application from scratch the chat works, but if I implement the code in my already existing application I get this error:

Missing Callback                                                   pubnub.min.js:1 

And I can't see messages that I write and messages that I should receive but I can send them and I can see those messages on the other side of the chat. Do you know how can I solve this problem?

EDIT: this is the controller for the pubnub chat:

'use strict';

angular.module('myApp.appointments', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
}])

.controller('appointmentsCtrl', ["$rootScope", "$scope", "$http", "$timeout", "$cookies", "URL", "Pubnub", function($rootScope, $scope, $http, $timeout, $cookies, URL, Pubnub) {

    $scope.sortType = 'name';
    $scope.sortReverse = false;

    $scope.sortType_s = 'time';

    $scope.filterAppointments = false;
    $scope.filterDate = '';

    $scope.highlightRow = '';

    $scope.$on('$routeChangeSuccess', function() {
        var data = {
            "token":$cookies.get('userToken'),
            "hairdresser_id": $cookies.get('userId')
        };

        $http.post(URL.url + 'appointments', data).then(function(res){
            $scope.app_list = res.data.appointments; 
            $scope.service_list = res.data.appointment_services; 
            $scope.customers_list = res.data.customers;
            $scope.pending_number = res.data.pending;
        });

        data = {
            "token":$cookies.get('userToken'),
            "hairdresser_id": $cookies.get('userId')
        };

        $http.post(URL.url + 'monthly_earnings', data).then(function(res){
            $rootScope.monthly_earnings = res.data.amount;
        });
    });

    // Pubnub implementation
    $scope.channel = "messages-channel";
    $scope.uuid =  _.random(100).toString();
    Pubnub.init({
        publish_key: MY_KEY,
        subscribe_key: SUB_KEY,
        ssl: true,
        uuid: $scope.uuid
    });

    $scope.sendMessage = function() {
        if (!$scope.messageContent  || $scope.messageContent === '') {
            return;
        }
        Pubnub.publish({
            channel: $scope.channel,
            message: {
                content: $scope.messageContent,
                sender_uuid: $scope.uuid,
                date: new Date()
            },
            callback: function(m) {
                 console.log(m);
            }
        });

        $scope.messageContent = '';
    }

    $scope.messages = [];

    Pubnub.subscribe({
        channel: $scope.channel,
        triggerEvent: ['callback']
    });

    $scope.$on(Pubnub.getMessageEventNameFor($scope.channel), function(ngEvent, m) {
        $scope.apply(function() {
            $scope.messages.push(m)
        });
    });

}]);
1 Answers
Related