Joey Yi Zhao
Joey Yi Zhao

Reputation: 42480

How to detect network going down event in feathersjs client

I am working on a nodejs project. The backend is using featuresjs and the client side is using feather-client js which is served by the backend. The backend code is shown as below:

app
  .use(compress())
  .options('*', cors())
  .use(cors())
  .use('/', serveStatic(app.get('public')))
  .use(bodyParser.json())
  .use(bodyParser.urlencoded({ extended: true }))
  .configure(hooks())
  .configure(rest())
  .configure(
    swagger({
      docsPath: '/docs',
      uiIndex: path.join(__dirname, '../public/docs.html'),
      info: {
        title: process.env.npm_package_fullName,
        description: process.env.npm_package_description,
      },
    }),
  )
  .configure(
    primus(
      {
        transformer: 'websockets',
        timeout: false,
      },
      (primus) => {
        primus.library();
        primus.save(path.join(__dirname, '../public/dist/primus.js'));
      },
    ),
  )
  .configure(services)
  .configure(middleware);

it works fine if the network is available on the host. But if the network going down and UI try to send a request to backend, that request will be cached in UI side. The request will be sent to backend when the network is going back online. Interesting thing is that this happens when my UI and backend running on the same machine and using localhost to connect each other. I don't understand why the network issue impacts localhost connection.

In addition, I wonder how I can detect the network unavailable in featuresjs client side. I want to show an error message to user if it is not availabe.

Upvotes: 0

Views: 364

Answers (1)

Atanas Palavrov
Atanas Palavrov

Reputation: 98

I'm doing it in by listening to the underneath socket events - see in the code:

/* Show the user that we are going to connect to the server.
 * Message box will disappear after initial state is read. */
coverBox.show("Connecting ...");

var socketio = io();

socketio.on("reconnecting", function(delay, attempt) {
    coverBox.show("Disconnected ... trying to reconnect.");
});

socketio.on("reconnect", function() {
    coverBox.hide();
});

socketio.on("reconnect_failed", function() {
    socketio.socket.reconnect();
});

var feathersApp = feathers()
    .configure(feathers.hooks())
    .configure(feathers.socketio(socketio))
    .configure(feathers.authentication({ storage: window.localStorate }));

Upvotes: 1

Related Questions