Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action Cable and AngularJS Cross Domain Integration

I currently have a Rails 5 application acting as my backend, we can call this the "Core." I also have another Rails 5 application acting as my frontend, which is serving up AngularJS client side, we can call this the "Front". These are two completely separate Rails 5 applications with completely different domains.

Basically, I am trying to integrate Action Cable through the Core and have it talk to the Front. I'm using this service here for the Front: https://www.npmjs.com/package/angular-actioncable. As far as the Core, that's just basic Action Cable set up.

Problem: I'm having some trouble getting the handshake to work across two separate domains. I've read through everything I can find online, unfortunately there is not much information. If you've done this before, please help!

Note: I do have Redis server running and I'm using separate ports to mimic the separate domains in development.

Core:

chat_channel.rb

class ChatChannel < ApplicationCable::Channel
  def subscribed
    stream_from 'http://localhost:2000/#/chat'
  end

  def unsubscribed
    stop_all_streams
  end

  def receive(data)
    ActionCable.server.broadcast('http://localhost:2000/#/chat', data)
  end

  def speak
    params.fetch('data').fetch('chat')
  end
end

route.js

mount ActionCable.server => '/cable'  

cable.yml

development:
  adapter: redis
  url: redis://localhost:6379

test:
  adapter: async

production:
  adapter: redis
  url: redis://localhost:6379

config/environments.rb

config.action_cable.disable_request_forgery_protection = true

Front:

ChatCtrl.js

app.controller('ChatCtrl', ['$scope', 'ActionCableChannel',
function($scope, ActionCableChannel) {

  $scope.inputText;
  $scope.chatData = [];

  var consumer = new ActionCableChannel("ChatChannel");
  var callback = function(message) {
    $scope.chatData.push(message);
  };

  consumer.subscribe(callback).then(function() {
    $scope.sendToMyChannel = function(message) {
      consumer.send(message, 'speak');
    };
    $scope.$on("$destroy", function() {
      consumer.unsubscribe().then(function() {
        $scope.sendToMyChannel = undefined;
      });
    });
  });

}
]);


// Action Cable Configuration
app.run(function (ActionCableConfig) {
  ActionCableConfig.wsUri = 'localhost:4000';
});

Error Message in Console:

enter image description here

like image 659
CarlsBad505 Avatar asked Nov 09 '22 07:11

CarlsBad505


1 Answers

Try

ActionCableConfig.wsUri = 'ws://localhost:4000';
like image 100
hiehuehue Avatar answered Nov 15 '22 06:11

hiehuehue