dimanche 23 octobre 2016

Connecting ActionCable to different host

I'm running a rails 5 app as a backend server, and an ember application for a front-end application. They are two separate applications hosted on two different domains - say, backend.dev and frontend.dev

The rails application has a simple connection class found at app/channels/application_cable/connection.rb that looks like the following:

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    def connect
      Rails.logger.debug("env: #{env.inspect}")
      Rails.logger.info("cookies.signed: #{cookies.signed.inspect}")
    end
  end
end

I have a simple base channel class at app/channels/application_cable/channel.rb with the following:

module ApplicationCable
  class Channel < ActionCable::Channel::Base
  end
end

And a single implementation of that class at app/channels/events_channel.rb:

class EventsChannel < ApplicationCable::Channel
  def subscribed
    Rails.logger.debug("env: #{env.inspect}")
    Rails.logger.info("cookies.signed: #{cookies.signed.inspect}")
    stream_from 'events'
  end
end

On the ember side of things, I'm using the actioncable js package from http://ift.tt/2f5VhwM. I've setup my consumer in my frontend by extending the controller class with the following:

cableService: Ember.inject.service('cable'),

setupConsumer: Ember.on('init', function() {
  let service = this.get('cableService');
  let consumer = service.createConsumer(`ws://backend.dev`);
  let channel = 'EventsChannel';

  consumer.subscriptions.create(channel, {
    disconnected() {
      Ember.debug(`${channel}#disconnected`);
    },

    connected() {
      Ember.debug(`${channel}#connected`);
    },

I'm fairly sure that my consumer is setup correctly, as I'm seeing some debug output when I get the following output to my js console:

DEBUG: EventsChannel#disconnected

However, I'm also seeing an odd error in the console as well:

WebSocket connection to 'ws://backend.dev/' failed: Error during WebSocket handshake: Unexpected response code: 200

I'm not sure what to make of the response code error here, and there's absolutely nothing being logged in my rails app. Is there anything additional that I need to setup to have actioncable work across domains? Any idea of what the 200 response code means here?




Aucun commentaire:

Enregistrer un commentaire