Consumer.js should be installed by default if ActionCable framework present

:studio_microphone: Is this thing still on?

Over the past 24 hours I’ve finally come to understand that all ActionCable channels which import consumer.js share the same connection to the server. This is a critical first step towards building a scalable AC-powered solution. The alternative is to call createConsumer directly and increase connection overhead linearly. I urge you to directly call out this insight in the Guide; it’s non-obvious that consumer is a memoized instance of a consumer until you’re forced to become an expert.

With apologies for burying the lede, I believe that if the ActionCable framework is present (not-skipped) during rails new, the app/javascript/channels folder should be created automatically. It should contain consumer.js and index.js. import 'channels' should be added to the application.js pack.

These files don’t exist until someone runs rails generate channel test for the first time, and since it’s easily possible to productively import and use createConsumer() in a Stimulus controller without ever running a generator, that means that there’s no consumer.js floating around for all of these consumers to import.

I propose that running a channel generator should only generate the test_channel.rb and test_channel.js, not the infrastructure for using ActionCable in a resource-effective way.

I think we already do all of this in Rails master. Here’s an example Rails app recently generated from master. It has, despite my not having generated any channels:

1 Like

As for the docs, PRs to improve them are very welcome.

1 Like

… of course they are. Thanks, @georgeclaghorn. Not sure how I got turned around on that point.

Now to figure out why some of our users don’t have these files.

What I don’t understand is what actioncable has to do with views I have an actioncable app that was working until I upgraded it to rails 7 Trying to debug it I found that I don’t understand actioncable at all I have a view that when the form is submitted to add a message to the conversation the controller issues an ActionCable.broadcast to a channel but why should the broadcast appear in the view What connects the channel to the view?

It’s really hard to parse and understand what you’re asking, friend. You don’t use any punctuation or line breaks, you ask three questions and state that you don’t understand ActionCable. This makes it a really daunting task to even know where to begin.

Technically, your question simply doesn’t belong in this thread because it is not related to the topic.

I suggest that you take a look at how some of the new Hotwire tooling in Rails 7+ has evolved. Specifically, you might look to the Turbo Streams functionality: Turbo Handbook

The TL;DR might be that Streams is using ActionCable to send <turbo-stream> tags to the client via a WebSockets broadcast. <turbo-stream> is a web component that performs an action on the client when it is received.

The conversations show view lists messages in the database and has a new message form at the bottom When the form’s submitted the message is added to the database and the log says ActionCable broadcasting to room channel but the page doesn’t change Seemingly because there’s no association between the page and the room channel

There’s no magic here.

I suggest that you either pull out the ActionCable broadcast commands from your controller action (or model class) and just let the page refresh (Turbo Drive can help here)… or take the time to learn ActionCable so that you can understand how this can all work together.

When I say that there’s no magic, I mean that all of these tools are composable and flexible, but without starting from some tutorials on how broadcasting to a channel works, you’re just going to be frustrated. What I can say is that these tools are powerful and fun, and will absolutely do what you need if you’re willing to accept that you have some homework to do. If you’re under a deadline, I would kill the websockets approach and just reload the page; think of WS as embellishing something simple that already works.

At any rate, this thread is not the correct place for this discussion as your inquiry has nothing to do with the topic. If you need further help, I suggest that you could join the StimulusReflex Discord or the GoRails Discord and ask for the help that you need.

1 Like