Actioncable translations not falling into the resulting JS function

I am trying to get a rails application together to replace the unreliable php monstrosity encoding. The current incarnation is pulling data from non-Rails db, pasting it into db Rails, and then displaying it in the views. Db is mostly filled with temperature readings that are added every few seconds. I can show a static page in Rails without problems, but trying to add ActionCable / realtime data was problematic. Everything seems to be working fine, but when I broadcast to my channel, it doesn't seem to get into the Received function in mychannel.coffee.

My setup: Server - Passenger (launch passenger launch) Job Search ActionCable - Redis

Data is imported from legacydb using a job that captures raw SQL and creates new records. After that, another task is broadcast on the channel.

The problems come from ActionCable, I think. It seems that all the examples that I can find require user input to run JS. However, I am trying to call 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 is working. I see that the broadcast falls into the dashboard_channel panel. However, nothing in dashboard.coffee is 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 incoming data on the websocket for this channel alert data['content'] 

Nothing happens. Logs show the broadcast, but nothing falls into the dashboard.coffee panel and triggers a warning in the browser. I think this is wrong because all the chat examples? Is there still a place where I receive the broadcast and push it to subscribers only when the server part changes?

If any other information is required to solve this problem, please let me know. This question made me think for several days.

+5
source share
2 answers

Check your frames first. Are you sure you get the messages you want?

enter image description here

Then on your channel you must set an identifier for your subscribers. If you have a stream associated with the model, then the broadcast used can be created from the model and the channel.

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

Then you can stream to your channel like this

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 bad practice because you cannot determine which user is transferring to your server.

+1
source

One thing I would add for troubleshooting and testing coffee / javascript is that console.log is your friend. Adding console.log "First step complete" , etc. Everywhere really helped keep track of where the errors were.

0
source

Source: https://habr.com/ru/post/1258792/


All Articles