Skip to main content
Service Worker support enables your wallet to run in the background, providing persistent connectivity and event handling. The ServiceWorkerWallet exposes getVtxoManager() for VTXO lifecycle management from the worker context.

Overview

Running your wallet in a service worker enables:
  • Background Processing: Keep your wallet active even when the main application is closed
  • Persistent Storage: Use IndexedDB for reliable data persistence
  • Event Handling: Process incoming payments and settlements in the background
  • Improved UX: Faster response times and better reliability

Quick Setup

The v0.4 SDK provides ultra-simplified service worker setup with automatic registration and identity management:

Service Worker Implementation

Create a service worker file that handles communication:
That’s all you need! The Worker class automatically:
  • Handles message routing
  • Manages wallet state
  • Processes events
  • Handles cleanup

Example: Storage and Identity Management

Here’s a complete example with storage and identity management:

Using IndexedDB Storage

For service workers, IndexedDB is recommended over LocalStorage:
IndexedDB is recommended for service workers because it’s accessible from both the main thread and service worker context, and can store larger amounts of data.

VTXO Management in the Service Worker

Available since v0.4.8. The VtxoManager is now accessible directly from the service worker wallet, enabling programmatic VTXO lifecycle operations alongside the automatic background renewal.
The service worker runs automatic VTXO renewal and boarding UTXO sweeps on startup. In addition, you can access the VtxoManager API directly for manual control:

How Background Automation Works

When the service worker starts, VtxoManager begins eagerly:
  1. Auto-renewal — scans for VTXOs within the expiry threshold and renews them automatically
  2. Boarding sweep — settles any pending boarding UTXOs into the Ark
All VTXO-submitting operations (send, settle, renewVtxos) are serialized internally to prevent race conditions. You don’t need to coordinate between background automation and user-initiated sends.
See VTXO Management for the full VtxoManager API including getExpiringVtxos, recoverVtxos, and getRecoverableBalance.

Handling Events

Service workers can process wallet events in the background:

Advanced: Custom Service Worker

If you need custom service worker logic, you can extend the Worker class:

Best Practices

Always use IndexedDB for service worker wallets:
Store your identity securely and load it on initialization:
Implement proper error handling for service worker operations. As of v0.4.8, the SDK exports explicit error types for clearer failure handling:

Troubleshooting

If the service worker fails to register:
  1. Ensure the service worker file path (serviceWorkerPath) is correct and accessible
  2. Service workers require HTTPS (or localhost for dev)
  3. Check browser console for registration errors
  4. Verify the service worker script imports Worker from @arkade-os/sdk and calls .start()
If you encounter IndexedDB errors:
  1. Check browser console for specific error messages
  2. Ensure the database name and version are consistent
  3. Clear browser data if the database schema changed
If you experience message timeouts:
  1. v0.4.8+: The SDK now auto-recovers from a dead service worker using a preflight PING before every operation. Persistent timeouts may indicate a network issue, not a crashed worker.
  2. Check that the Worker is properly started in the service worker file
  3. Verify network connectivity
  4. Check for errors in the service worker console
  5. If using custom error handling, check for WorkerError (worker unresponsive) vs WalletError (wallet-level failure) to distinguish root causes

Next Steps