ActionCable Server Broadcast is received when leaving the page












0















I'm noticing that my ActionCable messages are only appearing when I navigate away from the page that I should of seen them on... Here's a test controller:



class TestController < ApplicationController

before_action :run_before_action
after_action :run_after_action

def index
ActionCable.server.broadcast("test_channel", action: 'index')
end

private

def run_before_action
ActionCable.server.broadcast("test_channel", action: 'before_action')
end

def run_after_action
ActionCable.server.broadcast("test_channel", action: 'after_action')
end

end


Here's my Channel:



class TestChannel < ApplicationCable::Channel
def subscribed
stream_from "test_channel"
end

def unsubscribed
# Any cleanup needed when channel is unsubscribed
end
end


And then finally the JS to console log out the connections and data:



App.notifications = App.cable.subscriptions.create("TestChannel", {
connected: function () {
// Called when the subscription is ready for use on the server
console.log('connected');
},
disconnected: function () {
// Called when the subscription has been terminated by the server
console.log('disconnected');
},
received: function (data) {
// Called when there's incoming data on the websocket for this channel
console.log('received');
console.log(data);
$('body').append(data.action);
}
});


I'm using Redis for the adapter in development and I've removed Turbolinks.



But what I'm seeing is that on page load I only see the connected logged into the console. And the second I refresh the page, or click a link to navigate away, I see the received in the console, and the data appended to the page.



Why is this happening on leave? I've even made it so that the ActionCable broadcast happens before, during and after the action to see if it makes a difference... but it doesn't. Any ideas?



Update: interestingly if I open a new tab and open that page, I will see them in the original tab... and then on refresh it will show them again like before.










share|improve this question





























    0















    I'm noticing that my ActionCable messages are only appearing when I navigate away from the page that I should of seen them on... Here's a test controller:



    class TestController < ApplicationController

    before_action :run_before_action
    after_action :run_after_action

    def index
    ActionCable.server.broadcast("test_channel", action: 'index')
    end

    private

    def run_before_action
    ActionCable.server.broadcast("test_channel", action: 'before_action')
    end

    def run_after_action
    ActionCable.server.broadcast("test_channel", action: 'after_action')
    end

    end


    Here's my Channel:



    class TestChannel < ApplicationCable::Channel
    def subscribed
    stream_from "test_channel"
    end

    def unsubscribed
    # Any cleanup needed when channel is unsubscribed
    end
    end


    And then finally the JS to console log out the connections and data:



    App.notifications = App.cable.subscriptions.create("TestChannel", {
    connected: function () {
    // Called when the subscription is ready for use on the server
    console.log('connected');
    },
    disconnected: function () {
    // Called when the subscription has been terminated by the server
    console.log('disconnected');
    },
    received: function (data) {
    // Called when there's incoming data on the websocket for this channel
    console.log('received');
    console.log(data);
    $('body').append(data.action);
    }
    });


    I'm using Redis for the adapter in development and I've removed Turbolinks.



    But what I'm seeing is that on page load I only see the connected logged into the console. And the second I refresh the page, or click a link to navigate away, I see the received in the console, and the data appended to the page.



    Why is this happening on leave? I've even made it so that the ActionCable broadcast happens before, during and after the action to see if it makes a difference... but it doesn't. Any ideas?



    Update: interestingly if I open a new tab and open that page, I will see them in the original tab... and then on refresh it will show them again like before.










    share|improve this question



























      0












      0








      0








      I'm noticing that my ActionCable messages are only appearing when I navigate away from the page that I should of seen them on... Here's a test controller:



      class TestController < ApplicationController

      before_action :run_before_action
      after_action :run_after_action

      def index
      ActionCable.server.broadcast("test_channel", action: 'index')
      end

      private

      def run_before_action
      ActionCable.server.broadcast("test_channel", action: 'before_action')
      end

      def run_after_action
      ActionCable.server.broadcast("test_channel", action: 'after_action')
      end

      end


      Here's my Channel:



      class TestChannel < ApplicationCable::Channel
      def subscribed
      stream_from "test_channel"
      end

      def unsubscribed
      # Any cleanup needed when channel is unsubscribed
      end
      end


      And then finally the JS to console log out the connections and data:



      App.notifications = App.cable.subscriptions.create("TestChannel", {
      connected: function () {
      // Called when the subscription is ready for use on the server
      console.log('connected');
      },
      disconnected: function () {
      // Called when the subscription has been terminated by the server
      console.log('disconnected');
      },
      received: function (data) {
      // Called when there's incoming data on the websocket for this channel
      console.log('received');
      console.log(data);
      $('body').append(data.action);
      }
      });


      I'm using Redis for the adapter in development and I've removed Turbolinks.



      But what I'm seeing is that on page load I only see the connected logged into the console. And the second I refresh the page, or click a link to navigate away, I see the received in the console, and the data appended to the page.



      Why is this happening on leave? I've even made it so that the ActionCable broadcast happens before, during and after the action to see if it makes a difference... but it doesn't. Any ideas?



      Update: interestingly if I open a new tab and open that page, I will see them in the original tab... and then on refresh it will show them again like before.










      share|improve this question
















      I'm noticing that my ActionCable messages are only appearing when I navigate away from the page that I should of seen them on... Here's a test controller:



      class TestController < ApplicationController

      before_action :run_before_action
      after_action :run_after_action

      def index
      ActionCable.server.broadcast("test_channel", action: 'index')
      end

      private

      def run_before_action
      ActionCable.server.broadcast("test_channel", action: 'before_action')
      end

      def run_after_action
      ActionCable.server.broadcast("test_channel", action: 'after_action')
      end

      end


      Here's my Channel:



      class TestChannel < ApplicationCable::Channel
      def subscribed
      stream_from "test_channel"
      end

      def unsubscribed
      # Any cleanup needed when channel is unsubscribed
      end
      end


      And then finally the JS to console log out the connections and data:



      App.notifications = App.cable.subscriptions.create("TestChannel", {
      connected: function () {
      // Called when the subscription is ready for use on the server
      console.log('connected');
      },
      disconnected: function () {
      // Called when the subscription has been terminated by the server
      console.log('disconnected');
      },
      received: function (data) {
      // Called when there's incoming data on the websocket for this channel
      console.log('received');
      console.log(data);
      $('body').append(data.action);
      }
      });


      I'm using Redis for the adapter in development and I've removed Turbolinks.



      But what I'm seeing is that on page load I only see the connected logged into the console. And the second I refresh the page, or click a link to navigate away, I see the received in the console, and the data appended to the page.



      Why is this happening on leave? I've even made it so that the ActionCable broadcast happens before, during and after the action to see if it makes a difference... but it doesn't. Any ideas?



      Update: interestingly if I open a new tab and open that page, I will see them in the original tab... and then on refresh it will show them again like before.







      ruby-on-rails ruby actioncable






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 19 at 12:17







      Cameron

















      asked Jan 19 at 12:01









      CameronCameron

      8,94680231408




      8,94680231408
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54266873%2factioncable-server-broadcast-is-received-when-leaving-the-page%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54266873%2factioncable-server-broadcast-is-received-when-leaving-the-page%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Homophylophilia

          Updating UILabel text programmatically using a function

          Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage