Products Technologies Demo Docs Blog Support Company

Automatically Reconnect to the Server and Recover the Document

When a WebSocket connection drops, the TX Text Control editor automatically reconnects and recovers the document using browser local storage. The implementation saves document state client-side and restores it on reconnection, ensuring a seamless editing experience for end users.

Automatically Reconnect to the Server and Recover the Document

In order to provide the true WYSIWYG rendering and the unique pixel-perfect editing experience, the document is getting synchronized with the server using a WebSocket connection. A constant connection is required in order to maintain the document.

This sample shows how to use the local browser storage to save the document locally in order to recover the document when the connection is lost.

Reconnect to server

In the textControlLoaded event, the autoSaveDocument function is attached to an interval of 500 milliseconds. This is a background task and will not influence the user editing experience.

TXTextControl.addEventListener("textControlLoaded", function (e) {
      // disable the loading progress dialog
      TXTextControl.isLoadingDialogEnabled = false;

      // restore the document
      restoreDocument();

      // set auto save to 500 milliseconds
    var intervalAutoSave = setInterval(autoSaveDocument, 500);
});

In the autoSaveDocument function, the document is saved in the internal Text Control format and is stored in the local browser session storage. This storage is session-wide and can be accessed browser tab-wide. If you need to implement a "per document" session, you might want to give the session storage entry a unique name.

function autoSaveDocument() {
      // store the document in the session storage
      // which is shared over all browser tabs (complete session)
      TXTextControl.saveDocument(
          TXTextControl.streamType.InternalUnicodeFormat, function (e) {
              sessionStorage.document = e.data;
          }
      );
}

In case, the connection is lost, TX Text Control fires the event webSocketClosed. In this event, we simply reload the page in order to reconnect to the server.

TXTextControl.addEventListener("webSocketClosed", function (e) {
    // reload, if connection is lost
    location.reload();
});

In the textControlLoaded event (see first code snippet), the function restoreDocument is called. This function is loading the stored document from the session storage in case it is available.

function restoreDocument() {
      // if document exists, load it from the session storage
      if (sessionStorage["document"]) {

          $("#txReconnectMsg").show(); // hide dialog

        TXTextControl.loadDocument(
            TXTextControl.streamType.InternalUnicodeFormat,
              sessionStorage.document);
      }
      else
          $("#txReconnectMsg").hide(); // hide dialog
}

Other events are used to show and hide messages informing the user that the application is trying to reconnect to the server. The following snippet shows the complete JavaScript that is required to implement this feature:

TXTextControl.addEventListener("textControlLoaded", function (e) {
      // disable the loading progress dialog
      TXTextControl.isLoadingDialogEnabled = false;

      // restore the document
      restoreDocument();

      // set auto save to 500 milliseconds
    var intervalAutoSave = setInterval(autoSaveDocument, 500);
});

  TXTextControl.addEventListener("webSocketClosed", function (e) {
      // reload, if connection is lost
      location.reload();
  });

  TXTextControl.addEventListener("documentLoaded", function (e) {
      $("#txReconnectMsg").hide(); // show dialog
  });

  function autoSaveDocument() {
      // store the document in the session storage
      // which is shared over all browser tabs (complete session)
      TXTextControl.saveDocument(
          TXTextControl.streamType.InternalUnicodeFormat, function (e) {
              sessionStorage.document = e.data;
          }
      );
}

  function restoreDocument() {
      // if document exists, load it from the session storage
      if (sessionStorage["document"]) {

          $("#txReconnectMsg").show(); // hide dialog

        TXTextControl.loadDocument(
            TXTextControl.streamType.InternalUnicodeFormat,
              sessionStorage.document);
      }
      else
          $("#txReconnectMsg").hide(); // hide dialog
}

You can test this sample on your own by downloading the project from our GitHub repository.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

  • Visual Studio 2017 or better
  • TX Text Control .NET Server (trial sufficient)

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETReportingHTML5

Creating Your First ASP.NET Reporting Application

The MailMerge and ServerTextControl components of TX Text Control .NET Server for ASP.NET enable server-side reporting in Web Forms. A template.docx merges with XML data via a button click…


ASP.NETReportingGitHub

ASP.NET MVC: Implementing a Simplistic, Custom Button Bar

This ASP.NET MVC sample replaces the Web.TextControl ribbon bar with a custom button bar built in HTML, CSS, and JavaScript. Toggle buttons apply formatting commands like bold, while the…


ASP.NETReportingGitHub

ASP.NET MVC: Adding Protected Sections to Documents

SubTextParts in TX Text Control mark document regions as non-editable in Web.TextControl. An MVC controller method converts selected text into a SubTextPart using ServerTextControl, and…


ASP.NETReportingElectronic Signature

ASP.NET: Adding Electronic Signatures to Documents

An ASP.NET MVC sample demonstrates electronic signatures using an HTML5 canvas-based JavaScript signature pad. The captured signature image is sent to a controller action, which merges it into a…


ASP.NETReportingGitHub

HTML5: Display and Handle FormCheckBox Fields

MS Word FormCheckBox fields render in Web.TextControl by iterating ApplicationFields server-side and displaying Unicode checked or unchecked characters. The TextFieldClicked JavaScript event…

Share on this blog post on: