Skip to content

Manually reconnecting the web client #772

@lirbank

Description

@lirbank

Server version: 2.0.0-beta-7
Client version: 2.0.0-beta-7

Not sure if I am doing this the right way but I can't seem to reconnect to the server after a temporary disconnect. Even though the client can't reconnect on it own, it should be possible to reconnect manually without refreshing the browser. Am I missing something or does it not work?

I run the code below and then close the server and start it again.

Example client code:

let message = {
  text: "What a beautiful horizon!",
  datetime: new Date(),
  author: "@dalanmiller"
}

let horizon = Horizon();
let chat = horizon("messages");
let isConnected = false;
let handle;

document.addEventListener("DOMContentLoaded", function(event) {
  document.querySelector('button').addEventListener('click', e => {
    console.log('CLICK');
    chat.store(message);
  });

  chat.watch().subscribe(docs => console.log('DOCUMENTS', docs.length));

  horizon.status(s => {
    console.log('status', s);
  });

  horizon.onReady(s => {
    if (handle) {
      clearInterval(handle);
    }
  });

  horizon.onSocketError(e => {
    console.log('socket error', e);
    //horizon.disconnect();
    //horizon = null;

    handle = setInterval(() => {
      console.log('RECONNECTING');
      //horizon = Horizon();
      //horizon.connect();
      chat.watch().subscribe((docs) => {console.log(docs.map(e => e.text))});
    }, 10000);
  });
});

Console:
image

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions