27

I created the following using ActionCable but not able to receive any data that is being broadcasted.

Comments Channel:

class CommentsChannel < ApplicationCable::Channel
  def subscribed
    comment = Comment.find(params[:id])
    stream_for comment
  end
end

JavaScript:

var cable = Cable.createConsumer('ws://localhost:3000/cable');

var subscription = cable.subscriptions.create({
  channel: "CommentsChannel",
  id: 1
},{
  received: function(data) {
    console.log("Received data")
  }
});

It connects fine and I can see the following in the logs:

CommentsChannel is streaming from comments:Z2lkOi8vdHJhZGUtc2hvdy9FdmVudC8x

I then broadcast to that stream:

ActionCable.server.broadcast "comments:Z2lkOi8vdHJhZGUtc2hvdy9FdmVudC8x", { test: '123' }

The issue is that the received function is never called. Am I doing something wrong?

Note: I'm using the actioncable npm package to connect from a BackboneJS application.

Artem Kalinchuk
  • 6,502
  • 7
  • 43
  • 57

1 Answers1

67

Changing the cable adapter from async to redis in config/cable.yml fixed it for me.

Update

As Erem pointed out below, the server and console are isolated processes so you need to use a centralized queue manager.

Artem Kalinchuk
  • 6,502
  • 7
  • 43
  • 57
  • 1
    Awesome, had the same problem. Glad you posted the solution! Worked for me. – DiegoSalazar Sep 22 '16 at 21:46
  • 4
    @Artem It didn't work for me. Broadcasting is not working even if redis is used. The server console says: "[ActionCable] Broadcasting to room_channel: {:message=>"llllllllllll"}". But the received function is not called. I tried with both asyn adapter and redis adapter. – sufinsha Oct 10 '16 at 20:17
  • I don't know why it doesn't work for you but make sure the redis server is running and the redis host is set in `cable.yml`. – Artem Kalinchuk Oct 10 '16 at 22:18
  • http://stackoverflow.com/questions/35176934/actioncable-server-broadcast-from-the-console – kukrt Nov 15 '16 at 07:25
  • @sufinsha were you able to figure it out? I'm facing the same problem – shashwat Nov 20 '16 at 10:35
  • @sufinsha was it a typo? In my js I am connecting like `App.cable.subscriptions.create("NotificationsChannel", { ... }` and in my ruby I got `class NotificationsChannel < ApplicationCable::Channel`, that should be correct right? – Maxim Zubarev Jun 17 '17 at 09:09
  • 22
    I assume your broadcasting from your rails console? The server and the console are using isolated processes. You need to have a centralized queue manager. You need to use redis. – Erem Sep 29 '17 at 15:27
  • 2
    Solved for me as well. So strange. Thanks @ArtemKalinchuk – JohnSalzarulo Mar 07 '18 at 21:03
  • @Erem does that mean broadcasting from the console(like when testing a new channel) and the actual broadcast from the app's workflow require different adapters? Redis when in console, async when not? Sorry for this newbie inquiry. – Ricardo Green Nov 07 '19 at 05:35
  • testing from browser, instead of console fixed it for me – Jose Kj Jul 31 '21 at 04:25