How to implement reconnection and/or close connection in actioncable JS

60 views Asked by At

I have a node application that uses actioncable, the problem is the subscription is closed between 1hour and 2hours from the connection. I want to handle this by manually closing the connection then reconnect when I didn't receive a msg since some minutes.

The code:

cable.subscribe(msgUpdate, {
  connected() {
    console.log("connected");
  },

  disconnected(error) {
    console.log("disconnected", error);
  },

  rejected(error) {
    console.log("rejected", error);
  },

  received(data) {
    if (data?.result?.errors?.length > 0) {
      console.log("error", data?.result?.errors);
      return;
    }
    const msg = data?.result?.data;
    console.log(msg);
  },

Link of the implementation: https://github.com/sorare/actioncable

Thanks!

0

There are 0 answers