Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actioncable broadcasts not hitting Received JS function

I have been trying to get a rails app together to replace a nastily coded php monstrosity. The current incarnation is pulling data from a non-Rails db, putting it into the Rails db and then displaying it in the views. The db is mainly populated with temperature readings that are added every few seconds. I can display a static-ish page in Rails with no problems but, trying to add ActionCable/realtime data has proven problematic. MOST things seem to be working properly but, when I broadcast to my channel, it does not seem to hit the Received function in the mychannel.coffee.

My Setup: Server - passenger (bundle exec passenger start) Jobs - Resque ActionCable - Redis

Data is imported from the legacydb by a job that grabs the raw SQL and creates new records. After this, another job broadcasts to the channel.

The problems are coming from ActionCable, I think. All examples that I can find require user input to trigger the JS, it seems. However, I am trying to trigger things strictly from the server side. This job:

class DatabroadcastJob < ApplicationJob
  queue_as :default
  self.queue_adapter = :resque


  def perform
    ActionCable.server.broadcast 'dashboard_channel', content: render_thedata
  end

  private

    def render_thedata
      dataArr =[Data1.last, Data2.last, Data3.last]
      ApplicationController.renderer.render(partial: 
          'dashboard/data_tables', locals: {item: dataArr})
    end

end

Works. It works. I see the broadcast hitting the dashboard_channel. However, nothing in the dashboard.coffee gets triggered by the broadcast. This is incredibly confusing.

Dashboard.coffee

  App.dashboard = App.cable.subscriptions.create "DashboardChannel",
      connected: ->
    # Called when the subscription is ready for use on the server

      disconnected: ->
    # Called when the subscription has been terminated by the server

      received: (data) ->
      # Called when there's incoming data on the websocket for this channel
        alert data['content']

Nothing happens. The logs show the broadcast but nothing hits dashboard.coffee and raises an alert in browser. Am I thinking about this the wrong way because of all of the chat examples? Is there another place where I grab the broadcast and push it to subscribers when only making server side changes?

If any other info is needed to address this, please let me know. This issue has been driving me mental for days now.

like image 760
Scaylos Avatar asked Oct 26 '16 04:10

Scaylos


2 Answers

First, check your frames. Are you sure you're getting the messages you want?

enter image description here

Then, in your channel you should set an ID to your subs. If you have a stream that is related to a model, then the broadcasting used can be generated from the model and channel.

class DashboardChannel < ApplicationCable::Channel
  def subscribed
    post = Post.find(params[:id])
    stream_for post
  end
end

Then you can broadcast to your channel like so

DashboardChannel.broadcast_to(@post, @comment)

Otherwise, you should do the following:

class DashboardChannel < ApplicationCable::Channel
  def subscribed
    stream_from 'dashboard_channel'
  end
end

But this is a bad practice, because you won't be able to define which user transmits to your server.

like image 167
Viktor Avatar answered Nov 18 '22 12:11

Viktor


One thing I would add for troubleshooting and testing the coffee/javascript is that console.log is your friend. Adding console.log "First step complete" and so on throughout really helped to trackdown where the errors were occurring.

like image 1
Scaylos Avatar answered Nov 18 '22 14:11

Scaylos