Search code examples
ruby-on-rails-5turbolinksactioncable

ActionCable channel doesn't connect unless page is reloaded


I have a cable that works great if I manually refresh the page, but if I navigate to it, it never starts streaming.

WHen working correctly, I see this in the Rails console:

ChecklistPdfChannel is transmitting the subscription confirmation
ChecklistPdfChannel is streaming from checklist_pdf_2-5a-ntiasohen-gattopuin

Here's the coffeescript:

document.addEventListener 'turbolinks:load', ->
  if !App.presence
    App.presence = App.cable.subscriptions.create({
      channel: 'ChecklistPdfChannel',
      issueable_id: window.location.pathname.split('/').splice(2,1).toString()
    },
      connected: ->
      disconnected: ->
      received: (data) ->
        console.log(data)
        $('#pdf_status').html "#{data.html}"
  )
return

I've tried the following:

  • added meta tag: <meta name="turbolinks-cache-control" content="no-cache">
  • added 'data-turbolinks-track': 'reload' on the asset calls in the layout

the behavior is the same in both development and staging/production (Heroku).

Rails 5.2.2, ActionCable 5.2.2, Turbolinks 5.2.0


Solution

  • So the only way I was able to get this to work was to add this meta tag on the pages where I needed the cable active:

    <meta name="turbolinks-visit-control" content="reload">