4

I am using actioncable for a chat feature. It works perfectly when I test locally, but when I deploy to Heroku I get this error:

'WebSocket connection to 'wss://dg-automotive-portal- rails.herokuapp.com/cable' failed: Error during WebSocket handshake: Unexpected response code: 404'

production.rb:

  config.action_cable.url = 'wss://dg-automotive-portal- 
  rails.herokuapp.com/cable'
  config.action_cable.allowed_request_origins = [
  'https://dg-automotive-portal.herokuapp.com/', 'http://dg- 
   automotive-portal.herokuapp.com/' ]

cable.yml:

  production:
  adapter: redis
  url:redis:MYURL

routes.rb has

  mount ActionCable.server => '/cable'

And I have the redis gem installed. Does anyone know why this doesn't work when I deploy?

Javascript code called on componentWillMount:

  createSocket() {
      let cable = Cable.createConsumer('wss://dg-automotive-portal-rails.herokuapp.com/cable')
      this.chats = cable.subscriptions.create({
     channel: 'ChatChannel'
    }, {
      connected: () => {
        console.log("connected!")
     },
     received: (data) => {
       this.setState({
         chatLogs: [...this.state.chatLogs, data]
       })
     },
     create: function(chatContent, datetime, currentUser) {
       this.perform('create', {
        content: chatContent,
        time: datetime,
        user_id: currentUser
      })
    }
  })
}
rgc998
  • 299
  • 2
  • 15
  • Do you have Redis added to your Heroku instance? Also it would be helpful if you shared your configuration files. – Nitsew Mar 23 '18 at 18:07
  • Ahh never mind I see them now, sorry about that. – Nitsew Mar 23 '18 at 18:08
  • Yes i have it added to my heroku instance – rgc998 Mar 23 '18 at 18:11
  • It looks like the `config.action_cable.url` could be wrong. Try making the protocol `https` instead of `wss`. If this works I will add my answer so you can mark it as accepted. – Nitsew Mar 23 '18 at 18:13
  • Please see same question: https://stackoverflow.com/questions/41860718/action-cable-subscribing-locally-but-not-on-heroku – gwalshington Mar 23 '18 at 19:34
  • @Nitsew that didnt seem to work, I am getting the same exact error 'action_cable.js:241 WebSocket connection to 'wss://dg-automotive-portal-rails.herokuapp.com/cable' failed: Error during WebSocket handshake: Unexpected response code: 404' – rgc998 Mar 23 '18 at 19:56
  • @gwalshington that post doesnt help, I am making a request from the frontend which has a different url than my backend – rgc998 Mar 23 '18 at 20:05
  • We'll need to see the JavaScript you're using to make the websocket connection. – ArtOfCode Mar 24 '18 at 10:42
  • 1. Are you using Redis on the local machine as well (maybe the issue has nothing to do with Heroku)? 2. Do you have a custom URL for your app (it should be included in `config.action_cable.allowed_request_origins`)? 3. White space after `url:` in `url:redis:MYURL` might be nice... – Myst Mar 25 '18 at 16:26
  • @ArtOfCode ive added the javascript – rgc998 Mar 26 '18 at 14:06
  • @Myst 1. yes 2. yes i have custom URL and i added it in allow_request_origins but still get the same error. 3. white space made no difference, still the same error =/ – rgc998 Mar 26 '18 at 14:09
  • Hi, did you manage to have action cable to work on Heroku? I have the same issue with Rails 4 and Ember.js. Our Js code is similar. I am trying to use `disable_request_forgery_protection=true` without success either. Also tried with standalone server. – Bachet Feb 11 '19 at 17:35
  • Nevermind, I found out that allowed_request_origins was not working like in my development mode. Indeed I had a regex taken from an environment variable so I did the same on heroku with no success. Replacing regex by the array of plain strings worked. – Bachet Feb 12 '19 at 10:07

0 Answers0