Single Instance
Most desktop apps should only ever run one copy per user. When a user double-clicks the app icon while it’s already open, you want the existing window to focus — not a second window.
Install the required plugin:
tynd add single-instanceimport { app } from "@tynd/core/client";import { singleInstance } from "@tynd/plugin-single-instance/client";
const { acquired } = await singleInstance.acquire("com.example.myapp");
if (!acquired) { // We're the second instance. The primary has already auto-focused itself // and will receive {argv, cwd} via onSecondLaunch. Exit silently. await app.exit(0);}
// Only the primary reaches this point. Set up second-launch handling:singleInstance.onSecondLaunch(({ argv, cwd }) => { console.log("user tried to launch again with", argv, "from", cwd); // typical: focus the main window + interpret argv as a file-open request});acquire(id)uses a cross-OS exclusive lock + a per-user local socket for forwarding argv/cwd. The primary deliversapp:second-launchto app-local webviews only.- Hold the lock for the process lifetime — don’t release and re-acquire. The lock releases on process exit.
iddoubles as the OS lock name and the socket name. Use a stable reverse-DNS identifier.
What happens under the hood
Section titled “What happens under the hood”When acquire() returns { acquired: false }, the host has already:
- Connected to the primary instance’s local endpoint (an owner-only named pipe on Windows, a socket in a per-user
0700directory on Linux and macOS) and checked that it belongs to the current user. - Sent the forwarded payload as a single JSON line
{ argv, cwd }. - Triggered the primary window’s
setFocus()+ un-minimize via the host’s native event loop — no IPC round-trip needed.
Use cases
Section titled “Use cases”Deep-link handling
Section titled “Deep-link handling”Register a custom scheme in tynd.config.ts:
export default { backend: "backend/main.ts", frontendDir: "dist", protocols: ["myapp"], // myapp:// links now launch this app bundle: { identifier: "com.example.myapp" },} satisfies TyndConfig;Handle both cold-start and duplicate-launch cases with onOpenUrl:
singleInstance.onOpenUrl((url) => { // e.g. "myapp://invite/abc123" const parsed = new URL(url); router.navigate(parsed.pathname);});onOpenUrl fires for:
- Cold start — argv contains the URL.
- Duplicate launch — the primary receives the forwarded URL.
See Deep Linking for scheme registration details per OS.
File-open
Section titled “File-open”Users drag a file onto your app icon while it’s running. The OS relaunches the app with the file path as argv[1]:
singleInstance.onSecondLaunch(({ argv }) => { const file = argv.find((a) => a.endsWith(".myapp")); if (file) openFile(file);});Declare file type associations with bundle.fileAssociations in tynd.config.ts. The bundler writes the required .desktop, .app, and installer metadata.
Platform notes
Section titled “Platform notes”- Windows — owner-only named pipe, named from a hash of your user SID, the
idand a per-user, per-endpoint random secret (%LOCALAPPDATA%\tynd\pipe-nonce-<hash of the id>). Windows frees the pipe when the owning process dies; no stale-lock cleanup needed. - Linux and macOS — socket file in a per-user
0700directory. A file left by a crash is removed by the next primary instance, which holds the lock. - A second launch waits at most 2 s to connect to the primary; on timeout
acquire()rejects with aconnect:error and nothing is forwarded. - If the primary cannot create its forwarding endpoint, the app still starts but single-instance is not enforced (see the API page).
Error modes
Section titled “Error modes”acquire() returns false even when alone
Section titled “acquire() returns false even when alone”Stale lock. Usually self-heals on process exit (see above). If it doesn’t:
- Windows — nothing to do; Windows frees named pipes aggressively.
- Linux and macOS — the lock is a file lock, released by the OS when the process exits. If
acquire()still returnsfalse, another instance of the app is running for this user (killall MyAppto stop a hung one).
onSecondLaunch fires twice
Section titled “onSecondLaunch fires twice”You called singleInstance.onSecondLaunch(...) inside a handler that itself re-fires (React useEffect rerender, component re-mount). The API is idempotent for a given handler function, but registering two different handler functions means both fire.
// Register once, not in a render functionconst unsub = singleInstance.onSecondLaunch(onRelaunch);// call unsub() if you want to stop handlingId convention
Section titled “Id convention”Use a stable reverse-DNS identifier that matches bundle.identifier:
singleInstance.acquire("com.example.myapp");Don’t include version numbers — a user upgrading from 1.0.0 to 1.1.0 expects the upgrade to auto-focus the already-running old version (so the old one can shut down cleanly, then the new one starts). Different ids break that.
What this doesn’t give you
Section titled “What this doesn’t give you”- Multi-user isolation on Windows (service / tenancy setups) —
single-instanceis user-scoped, so two users on the same box each get their own primary. - Cross-machine coordination — no network-wide lock. If you want a license server, build your own.