Using WebSocket to Enable Page‑App Communication for Conditional App Launch or Download

The article explains how to use a custom URL schema together with a WebSocket channel to let a web page detect whether a mobile app is installed, attempt to launch it, and fall back to downloading the app when the launch fails, including practical blur‑event tricks.

UC Tech Team
UC Tech Team
UC Tech Team
Using WebSocket to Enable Page‑App Communication for Conditional App Launch or Download

In many real‑world scenarios developers need to prompt users to download a mobile app, but if the app is already installed they want to open it directly. Since JavaScript cannot directly check app installation, the article proposes a workaround using a custom URL schema and a WebSocket link between the web page and the native app.

The Android app must expose a WebSocket server (e.g., on port 8899). The web page’s JavaScript establishes a WebSocket connection to this server. When the user clicks the download button, the page calls download(schema, downUrl) which attempts to invoke the app via its schema.

If the app is running, the WebSocket connection allows the page to communicate with it; otherwise the page must decide whether the call succeeded. The author suggests using a short setTimeout and checking whether the browser lost focus (blur) as an indicator that the app was launched.

Two methods for detecting the blur are described: listening to the window.blur event or monitoring the document.visibilityChange event and checking the document.hidden property. Sample code snippets (shown as images in the original article) illustrate both the simple and compatible implementations.

By combining the blur detection with the WebSocket communication, the page can reliably decide: if the app launch succeeds, the browser stays idle; if it fails, the page proceeds to download the app from the provided URL. The article also notes a limitation—if the app is not alive in the background process, the call cannot succeed, requiring additional work to keep the app resident.

The author invites readers to discuss any errors and promotes the “UC International Technology” public account for more high‑quality technical articles.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

frontendJavaScriptWebSocketmobile appApp Deep LinkingDownload Prompt
UC Tech Team
Written by

UC Tech Team

We provide high-quality technical articles on client, server, algorithms, testing, data, front-end, and more, including both original and translated content.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.