ActionCable: keep subscription in another Ctrl

5 views
Skip to first unread message

Valen Onish

unread,
May 14, 2018, 3:07:56 PM5/14/18
to Angular and AngularJS discussion
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 consumer = new ActionCableChannel("NotifyChannel", {user_id: myConfig.currUser.id} );
  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; });
    })
  })
});
Reply all
Reply to author
Forward
0 new messages