Home > Enterprise >  How to listen close event in pusher js
How to listen close event in pusher js

Time:09-17

We are trying to trigger events based on channel outputs created in pusher

import Pusher from "pusher-js";
import { logoutUser } from "../redux/actions/authActions";
import store from "../redux/store/store";

const authSessionPusher = (sessionId) => {
  const pusher = new Pusher("xxxxxxxxxxxxxxxx", {
    cluster: "ap2",
    encrypted: true
  });
  const channel = pusher.subscribe(sessionId);
  console.log(channel);
  channel.bind("authSession", (data) => {
    if (data.message === "session logout") {
      store.dispatch(logoutUser());
    }
  });
};

export { authSessionPusher };

We are able to do this but now we also want to invoke an event when the pusher channel connection timeouts

What type of event listener should we add to get a callback on channel connection time out to attach function on that event?

CodePudding user response:

It's possible to bind to connection events. They are documented here: https://pusher.com/docs/channels/using_channels/connection/#connection-states

You can bind to a specific connection event (list of available states).

pusher.connection.bind("connected", function () {
  console.log("Connected!");
});

Or alternatively bind to all connection state changes by binding to state_change.

pusher.connection.bind("state_change", function (state) {
  console.log("Connection state", state);
});
  • Related