Nick M
Nick M

Reputation: 2532

Detect Action Cable connection status

I have a Vue app that connects to a Rails back-end and need to find out whether the connection is established or not.

In my app I do:

var app = new Vue({
    name: "Site",
    store,
    router,
    i18n,
    components: {},
    mixins: [],
    data: {
     cable: App.cable
    }
});

However cable.connection.disconnected is always true even when actually connected.

How can I reliably figure out (when needed) what the connection status is?

Upvotes: 3

Views: 1418

Answers (2)

Nick M
Nick M

Reputation: 2532

Got it sorted out. In my components, when I need to find out cable status, I do

this.$root.cable.connection.isOpen()

If false I revert to HTTP.

Hope this helps someone later on.

Upvotes: 2

Bùi Nhật Duy
Bùi Nhật Duy

Reputation: 322

You can try broadcast some message on server and watch Vue app can receive that message.

Upvotes: -1

Related Questions