Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionCable channel not accessible outside rails plugin

I'm trying to create a gem which exposes an ActionCable channel, but I can't get it to work.

This is my gem

# lib/my_channel.rb
class MyChannel < ActionCable::Channel::Base
  def wait(data)
    # logic ...
  end
end

# lib/engine.rb
module MyApp
  class Engine < ::Rails::Engine
    isolate_namespace MyApp
  end
end

I then add the gem to my main applications Gemfile, run bundle install, start up the console and run MyChannel. Which don't yield and error, meaning that the channel as been included properly.

I then add this to my main application

// application.js
var socket = "ws://localhost:3000/cable";
var cable = ActionCable.createConsumer(socket);
cable.subscriptions.create({ "channel": "MyChannel" }, {
  received: function(){
    // ...
  }
});

But I'm getting this error

Subscription class not found ({"command"=>"subscribe", "identifier"=>"{\"channel\":\"MyChannel\"}"})

What am I missing?

like image 361
Linus Oleander Avatar asked Jan 13 '16 01:01

Linus Oleander


1 Answers

This answer doesn't apply to gems, only making channels within a Rails app.

Your my_channel.rb is located in the wrong place. I'm not sure if behaviour has changed between the betas (I'm using beta3), but it should be located in app/channels/application_cable/.

I was experiencing the same issue, and realized my channel file was named inappropriately (host.rb instead of host_channel.rb). After renaming the file, it started working.

I believe the channel files are only searched for with the specific location and naming scheme (app/channels/application_cable/*_channel.rb) by default.

like image 142
ollpu Avatar answered Oct 20 '22 01:10

ollpu