# 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.

- **Author:** Bjoern Meyer
- **Published:** 2019-09-05
- **Modified:** 2026-07-17
- **Description:** 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.
- **3 min read** (474 words)
- **Tags:**
  - ASP.NET
  - Auto Save
  - HTML5
  - Reporting
- **Web URL:** https://www.textcontrol.com/blog/2019/09/05/automatically-reconnect-to-the-server-and-recover-the-document/
- **LLMs URL:** https://www.textcontrol.com/blog/2019/09/05/automatically-reconnect-to-the-server-and-recover-the-document/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2019/09/05/automatically-reconnect-to-the-server-and-recover-the-document/llms-full.txt
- **GitHub Repository:** https://github.com/TextControl/TextControl.Web.MVC.Reconnect

---

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](https://s1-www.textcontrol.com/assets/dist/blog/2019/09/05/a/assets/reconnect.webp "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.

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [Creating Your First ASP.NET Reporting Application](https://www.textcontrol.com/blog/2020/01/01/creating-your-first-aspnet-reporting-application/llms.txt)
- [ASP.NET MVC: Implementing a Simplistic, Custom Button Bar](https://www.textcontrol.com/blog/2017/03/13/aspnet-mvc-implementing-a-simplistic-custom-button-bar/llms.txt)
- [ASP.NET MVC: Adding Protected Sections to Documents](https://www.textcontrol.com/blog/2017/03/01/aspnet-mvc-adding-protected-sections-to-documents/llms.txt)
- [ASP.NET: Adding Electronic Signatures to Documents](https://www.textcontrol.com/blog/2016/10/12/aspnet-adding-electronic-signatures-to-documents/llms.txt)
- [HTML5: Display and Handle FormCheckBox Fields](https://www.textcontrol.com/blog/2015/09/22/html5-display-and-handle-formcheckbox-fields/llms.txt)
- [C# Document Generation: A Developer's Guide for .NET](https://www.textcontrol.com/blog/2026/07/08/csharp-document-generation-developer-guide-for-dotnet/llms.txt)
- [Convert SSRS RDL Reports to DOCX and TX Text Control Templates in .NET C#](https://www.textcontrol.com/blog/2026/06/22/convert-ssrs-rdl-reports-to-docx-and-tx-text-control-templates-in-dotnet-csharp/llms.txt)
- [.NET C# PDF Generation Techniques: Which Method is Right for Your Project?](https://www.textcontrol.com/blog/2024/10/30/net-csharp-pdf-generation-techniques-which-method-is-right-for-your-project/llms.txt)
- [Best Practices for Mail Merge and Form Field Processing in ASP.NET Core C# Applications](https://www.textcontrol.com/blog/2024/07/30/best-practices-for-mail-merge-and-form-field-processing-in-asp-net-core-csharp-applications/llms.txt)
- [Advantages of Flow Type Layout Reporting vs. Banded Reporting or PDF Template Engines in .NET C#](https://www.textcontrol.com/blog/2024/07/29/advantages-of-flow-type-layout-reporting-vs-banded-reporting-or-pdf-template-engines-in-net-c-sharp/llms.txt)
- [TX Text Control 32.0 Has Been Released](https://www.textcontrol.com/blog/2023/09/13/tx-text-control-320-has-been-released/llms.txt)
- [TX Text Control 31.0 and TX Spell .NET 10.0 Have Been Released](https://www.textcontrol.com/blog/2022/09/07/tx-text-control-31-released/llms.txt)
- [TX Text Control 30.0 and TX Spell .NET 9.0 Have Been Released](https://www.textcontrol.com/blog/2021/11/29/tx-text-control-30-released/llms.txt)
- [Detect Toggle Button Changes Using a MutationObserver](https://www.textcontrol.com/blog/2021/11/11/detect-toggle-button-changes-using-a-mutationobserver/llms.txt)
- [TX Text Control X19 and TX Spell 8.0 Have Been Released](https://www.textcontrol.com/blog/2020/12/02/tx-text-control-x19-released/llms.txt)
- [Implementing Conditional Table Cell Colors with MailMerge](https://www.textcontrol.com/blog/2020/10/08/implementing-conditional-table-cell-colors-with-mailmerge/llms.txt)
- [Securing the WebSocketHandler Endpoint in ASP.NET](https://www.textcontrol.com/blog/2020/05/28/securing-the-websockethandler-endpoint-in-aspnet/llms.txt)
- [MVC NuGet Packages for X18 Published](https://www.textcontrol.com/blog/2020/03/17/mvc-nuget-packages-for-x18-published/llms.txt)
- [TX Text Control X18 has been Released](https://www.textcontrol.com/blog/2020/03/16/tx-text-control-x18-released/llms.txt)
- [Sneak Peek X18: Form Field Conditional Instructions](https://www.textcontrol.com/blog/2020/01/03/sneak-peek-x18-form-field-conditional-instructions/llms.txt)
- [DocumentViewer: New Two Page View Mode](https://www.textcontrol.com/blog/2020/01/02/documentviewer-new-two-page-view-mode/llms.txt)
- [Creating an ASP.NET MVC DocumentViewer Application With Razor](https://www.textcontrol.com/blog/2020/01/01/creating-an-aspnet-mvc-documentviewer-application-with-razor/llms.txt)
- [Creating an ASP.NET MVC Application With Razor](https://www.textcontrol.com/blog/2020/01/01/creating-an-aspnet-mvc-application-with-razor/llms.txt)
- [Creating an ASP.NET Web Forms AJAX Application](https://www.textcontrol.com/blog/2020/01/01/creating-an-aspnet-web-forms-ajax-application/llms.txt)
- [MailMerge Class Settings Explained](https://www.textcontrol.com/blog/2019/12/05/mailmerge-settings-explained/llms.txt)
