anycable / anycable-rails

AnyCable for Ruby on Rails applications
https://anycable.io
MIT License
499 stars 35 forks source link

config.action_cable.url in development is being overwritten somewhere #115

Closed tambakoo closed 4 years ago

tambakoo commented 4 years ago

Environment ruby 2.5.0p0 Rails 6.0.0.rc2 AnyCable v0.6.4 anycable-rails (0.6.5) grpc (1.27.0)

I set up anycable using the official documentation but I kept getting a ActionController::RoutingError (No route matches [GET] "/cable"):. The url being knocked on was ws://localhost:3000/cable while the anycable-go websocket server is running on port 3334. I use webpacker on this app. This routing error is only fixed when in the app/javascript/channels/consumer.js I use

import { createConsumer } from "@rails/actioncable"
export default createConsumer('ws://localhost:3334/cable')

that is, explicitly declare the url while creating the consumer.

It does not matter if I remove the config.action_cable_url from development.rb which is very weird. This is clearly a case of overwritten configuration.

This error was not resolved by #88 or #41. Does this look like a webpacker problem ? Please tell me what files will be relevant to debug this.

palkan commented 4 years ago

Hey!

Do you use the action_cable_meta_tag helper in your HTML? And if yes, is it called before the JS script is loaded?

Action Cable uses meta tags (set by action_cable_meta_tag) to get the value of config.action_cable.url (NOTE: it is action_cable.url not action_cable_url).

tambakoo commented 4 years ago

Hey!

Do you use the action_cable_meta_tag helper in your HTML? And if yes, is it called before the JS script is loaded?

Action Cable uses meta tags (set by action_cable_meta_tag) to get the value of config.action_cable.url (NOTE: it is action_cable.url not action_cable_url).

  1. This is how my application.html.erb looks like -

    <!DOCTYPE html>
    <html>
    <head>
    <title>CapApi</title>
    <%= action_cable_meta_tag %>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>
    
    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
    </head>
    
    <body>
    <%= yield %>
    </body>
    </html>
  2. My bad, I meant action_cable.url. It's written correctly on the code.
palkan commented 4 years ago

Thanks!

And what do you see in the rendered HTML then? Does it contain the valid link (localhost:3334)?

tambakoo commented 4 years ago

Thanks!

And what do you see in the rendered HTML then? Does it contain the valid link (localhost:3334)?

yes there is a <meta name="action-cable-url" content="ws://localhost:3334/cable"> on the rendered HTML.

palkan commented 4 years ago

The only suggestion is to debug the JS createConsumer function and see where it gets the URL from if it’s not provided. It should be from the meta tag 🤷🏻‍♂️