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:
Try
ActionCableConfig.wsUri = 'ws://localhost:4000';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With