Subscription class not found: "room_channel" | Rails 5 API project actioncable angular 4/2

时间:2017-06-15 09:23:09

标签: ruby-on-rails angular actioncable ruby-on-rails-5

I am getting following error

Subscription class not found: "room_channel"

on my Rails API only project which contains no assets/scripts. I generated a Cable and Channel and mounted its rout as

mount ActionCable.server, at: '/cable'

Model:

class Discussion < ApplicationRecord
    after_commit { ActionCable.server.broadcast "room_channel", as_json.merge(message: 'test values for ActionCable')}
end

When a discussion is created, it broadcast:

[ActionCable] Broadcasting to room_channel: {"id"=>40, "description"=>"new ", "created_at"=>Thu, 15 Jun 2017 09:13:06 UTC +00:00, "updated_at"=>Thu, 15 Jun 2017 09:13:06 UTC +00:00, "avatar_url"=>nil, "full_name"=>nil, "job_title"=>nil, "company"=>nil, "email"=>nil, "msisdn"=>nil, :message=>"test values for ActionCable"}

on Angular 4 client app I am using package for actioncable:

ng2-cable

app.component.ts

import { Component } from '@angular/core';
import { Ng2Cable, Broadcaster } from 'ng2-cable';

@Component({
  moduleId: module.id,
  selector: 'sd-app',
  templateUrl: 'app.component.html',
  providers:[Ng2Cable, Broadcaster] //or add to main @NgModule
})

export class AppComponent {
  constructor(private ng2cable: Ng2Cable,
              private broadcaster: Broadcaster) {
    this.ng2cable.subscribe('http://localhost:1337/cable', 'room_channel');

    this.broadcaster.on<string>('room_channel').subscribe(
      message => {
        console.log(message);
      }
    );
  }
}

0 个答案:

没有答案