ActionCable: keep subscription in another Ctrl

5 visualizações
Pular para a primeira mensagem não lida

Valen Onish

não lida,
14 de mai. de 2018, 15:07:5614/05/2018
para 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; });
    })
  })
});
Responder a todos
Responder ao autor
Encaminhar
0 nova mensagem