I have chatCtrl.js where consumer subscribes to NotifyChannel.
Also I have mapCtrl.js and a user have to switch between chatCtrl.js and mapCtrl.js
Problem: when switching from chatCtrl.js to mapCtrl.js, I don't want to destroy subscription in chatCtrl.js and user should get messages even when he is in mapCtrl.js
I tried $rootScope.consumer instead of 'var consumer', but it's a bad idea.
I use angular 1.5.11
Here, my
chatCtrl.js
app.run(function (ActionCableConfig){
ActionCableConfig.debug = true;
//ActionCableConfig.wsUri= "ws://localhost:3000/cable";
});
app.controller('chatCtrl', function($scope, myConfig, $routeParams, ActionCableChannel, ChatService, SharedState, ActionCableSocketWrangler){
var callback = function(message) {
myConfig.chatData.push(message);
};
consumer.subscribe(callback).then(function(){
$scope.sendToMyChannel = function(message){
consumer.send(response.data);
};
$scope.$on("$destroy", function(){
consumer.unsubscribe().then(function(){ $scope.sendToMyChannel = undefined; });
})
})
});