Self-hosted source synchronization
Self-hosted source synchronization keeps IPTV source definitions in a selected server library and gives each browser or Electron client a usable local projection. The server owns the shared source record. The client materializes source content for its runtime while retaining local viewing state and valid provider sessions.
Synchronization boundary
Source ownership
The self-hosted server is the authority for a source's identity, title, kind, connection details, and source payload. A client can edit these fields, but the server accepts the edit only against the revision that the client last read. This prevents an old client from silently replacing a newer source.
The source record contains portable connection data for four source kinds:
| Source kind | Shared definition |
|---|---|
| M3U URL | URL and optional request headers |
| M3U text | The raw M3U text |
| Xtream | Server URL and account credentials |
| Stalker | Portal, MAC address, and approved device details |
Runtime fields do not cross this boundary. Favorites, recent items, playback state, parsed catalog rows, and provider login state remain client-side for source synchronization. Other self-hosted storage services can manage provider user data through their own contract.
References: libs/…/synced-playlist-store.ts• L24-30 libs/…/source-row-snapshot.ts• L11-25
Client modes
Browser and Electron clients use the same synchronization service, but they use different transport paths. The browser sends guarded HTTP requests. Electron sends the same narrow request types through its native bridge. Both paths verify the server before they send credentials or source edits.
The client keeps its local source projection in a profile tied to the server instance. This separates sources and user data from another server that happens to use the same browser or machine. A disconnected client returns to its normal local data mode after restart or reload.
References: apps/…/source-sync.providers.ts• L29-53 libs/…/source-connection.ts• L21-28 apps/…/source-sync.events.ts• L110-135
Server connection
Connection setup
Connection setup first checks that the target is an IPTVnator server with server storage and the source-sync capability. It records the server API address and stable database instance ID. The access token is kept separately from that connection record.
In the browser, the client can discover the runtime backend or use a server selected in settings. In Electron, the native process validates the server and can relaunch the app so all local services use the new source profile. The settings page can also preview local sources and create copies in the server library.
References: libs/…/source-connection.ts• L30-45 libs/…/source-connection.ts• L47-131 apps/…/settings-server-section.component.ts• L76-115 apps/…/settings-server-section.component.ts• L142-170 apps/…/source-sync.events.ts• L136-200
Request authorization
The instance ID is an identity check, not only a server address check. The client verifies it before it sends a token or provider credentials. A failed network or gateway request revokes that proof, so the next protected request must verify the instance again.
The server rejects requests from a client that presents a different instance ID. This protects a client from writing to a replacement database at the same URL. The browser also applies the same proof to provider requests routed through the selected self-hosted backend.
The source API maps source, favorite, and activity operations to narrow server routes. Browser requests carry the instance ID and authorization data only after the request passes the source authorization check. Source creation uses an absent-resource condition, while inventory reads and source updates use revision conditions. A network failure, gateway failure, invalid server response, unauthorized response, or instance mismatch invalidates the verification proof. A normal source conflict remains a conflict and does not revoke the proof.
References: libs/…/source-authorization.ts• L10-71 libs/…/source-api.ts• L16-59 libs/…/source-api.ts• L61-191 apps/…/source-proxy.ts• L26-77 apps/…/sources-router.ts• L20-35 apps/…/source-sync.events.ts• L50-107
Local source projection
Portable source definitions
The client converts a playlist into a portable source definition before it sends it. This conversion selects only the fields needed to retrieve or interpret the source. Validation rejects unknown fields, invalid source-kind combinations, unsafe URLs, and missing credentials.
Source fingerprints identify the same source across client and server copies. They use the source kind and stable connection identity rather than local activity. When a create request matches an existing fingerprint, the server returns the existing source as the canonical record instead of creating a duplicate.
References: libs/…/source-definition.util.ts• L22-57 libs/…/source-definition.util.ts• L145-217 libs/…/source-definition.util.ts• L219-247 apps/…/sources-router.ts• L101-146
Source materialization
Materialization turns a server definition into the local playlist shape used by the rest of the app. It preserves local activity while applying the server's source fields. M3U text is parsed locally. An M3U URL is fetched and parsed when its local content is missing, stale, or marked for retry. Xtream and Stalker definitions become ready for their provider workspaces without copying provider catalog data into the source inventory.
If an M3U URL changes, the client clears the old channel projection before it fetches the replacement. This prevents channels from the old provider from appearing under the new source. If the fetch fails, the client keeps the source with an empty or pending projection instead of showing stale channels. A later refresh can retry the fetch. A changed Stalker connection clears an old provider session unless the session still proves that it belongs to the accepted connection.
References: libs/…/source-materializer.ts• L17-92 libs/…/source-stalker-session.ts• L7-23 libs/…/source-definition.util.ts• L90-115
Local state retention
The local projection is more than a copy of the server row. When a server update arrives, the client retains activity and settings that the update did not change. This includes favorites, recent items, playback positions, refresh choices, group visibility, and local EPG choices.
A provider session is live login and runtime state used to access a provider. The client does not send that state as part of source synchronization. It retains a valid Stalker session only when its identity still matches the accepted source connection. A connection edit or a stale projection cannot restore an obsolete session.
EPG state also has local ownership rules. The client keeps detected guides and device-added guides separate, so a delayed projection does not restore a guide that the user removed. Older profiles can receive one-time activity migration from the legacy playlist API, but new devices do not use that API.
Source-associated favorite context is also retired when a source connection changes or a source is deleted. A later source with the same ID cannot reuse that old context.
References: libs/…/retain-source-projection.ts• L5-45 libs/…/source-row-snapshot.ts• L35-103 libs/…/source-stalker-session.ts• L7-23 libs/…/legacy-playlist-activity.ts• L15-89 libs/…/source-sync.service.ts• L90-116 libs/…/synced-playlist-store.ts• L265-277
Client projection updates
The web runtime connects completed synchronization changes to the active application workspace. Before it handles changes, it starts the consented favorites and activity runtimes for the selected source server. Each change reloads the shared source list, hydrates the active M3U workspace only when its row still exists and the route is unchanged, and sends a removed source back to the source list. After initial synchronization completes, it starts the consented history runtime. Optional runtime failures do not block source synchronization.
References: apps/…/source-sync-runtime.ts• L15-61
Inventory refresh
Refresh cycle
Refresh reads the server inventory and brings the local projections to the same source set. The client serializes refresh and source writes. It also shares the work across browser tabs when the runtime provides a cross-tab lock.
The refresh cycle follows this order:
- Finish any pending local cleanup.
- Verify the server instance.
- Ask for the source inventory with the last collection revision unless a pending source projection requires a full read.
- Skip the large inventory read when the server returns
304 Not Modified. - Materialize changed or pending sources.
- Remove local projections that the server no longer owns.
- Save the new collection revision and publish the changed source list.
The client starts a refresh at startup and can resume it after focus, visibility, network, or system-resume events. Failed refreshes use a backoff delay. The client marks the projection ready and publishes its change only after it saves the initialization receipt and collection revision. A failed local receipt write therefore remains retryable instead of presenting a false empty or complete library. The status surface exposes retry, token repair, and disconnect actions when the client cannot reach or authorize the server.
This diagram shows how a server inventory becomes a safe local source set.
flowchart TD
A[Configure server] --> B[Verify instance]
B --> C[Read source inventory]
C -->|304| D[Keep current projections]
C -->|changed inventory| E[Read full M3U text when needed]
E --> F[Materialize changed sources]
F --> G[Reconcile additions and updates]
G --> H[Prune missing sources by revision]
H --> I[Publish local source list]
References: libs/…/source-sync.service.ts• L77-151 libs/…/source-sync.service.ts• L153-259 libs/…/source-api.ts• L61-191 apps/…/source-sync-status-view.component.ts• L17-73
Revision reconciliation
Each source has a revision tag. The inventory has a collection revision. The client compares both before it applies a change, so a late response cannot replace a newer local projection.
When the server adds or changes a source, the client projects the new definition. When the server removes a source, the client prunes the local row only if the row still has the revision that was observed. If the row changed during the read, the prune is skipped rather than deleting newer local work. M3U text is fetched by source ID because the list response can omit its large payload.
References: libs/…/source-inventory-reconciliation.ts• L11-67 libs/…/playlist-revisions.ts• L9-38 apps/…/sources-router.ts• L78-99
Source edits
Conflict-safe writes
A local source edit sends only the source definition with the revision from its last accepted server copy. Creates use an absent-source condition. Updates use a matching revision. The server returns a conflict when another client changed the source first, and the client keeps the conflict state until it refreshes.
The client merges an accepted server response with local activity. A title-only edit does not need to rebuild the parsed source. A connection edit can rebuild source content and must not keep provider state tied to the old connection. Each read also carries a local snapshot, so a delayed activity write cannot replace newer channels, EPG state, or provider session changes that arrived after the read. If that snapshot is stale, the write is rejected so the caller can retry from a fresh read.
References: libs/…/synced-playlist-store.ts• L131-232 libs/…/source-row-snapshot.ts• L28-103 apps/…/sources-router.ts• L147-169 apps/…/sources-router.ts• L53-65
Source cleanup
Deletion ownership
Deletion follows server ownership. A single-source delete or clear first changes the shared server inventory, then removes matching local projections and runs cleanup hooks for related local state. Refresh and mutation paths retry unfinished cleanup before they accept a source projection, so a source ID cannot be reused while its earlier cleanup is unfinished.
Cleanup uses durable per-source markers. The markers survive a deleted row, an app restart, and concurrent browser tabs. If storage or a cleanup hook fails, the marker remains and the next sync retries it. Revision checks stop cleanup for an old delete from removing a newer row that reused the same ID.
References: libs/…/source-cleanup-queue.ts• L6-16 libs/…/source-cleanup-queue.ts• L22-69 libs/…/source-cleanup-queue.ts• L71-133 libs/…/source-sync.service.ts• L153-163 libs/…/source-sync.service.ts• L297-312 libs/…/synced-playlist-store.ts• L239-263
Change checklist
When these files or sections are changed, remember to consider these:
- Keep source kinds, allowed fields, validation rules, and fingerprints aligned between source-definition.util.ts, the shared source contract, and the server routes.
- Keep source operations mapped to the same HTTP methods and conditional headers in the browser API and Electron bridge.
- Keep collection and per-source revision handling aligned across list reads, create/update/delete requests, and local prune checks.
- Keep materialization, row snapshots, and projection retention aligned so source edits do not erase local activity, EPG provenance, or a valid provider session.
- Keep browser profile naming, instance checks, token handling, and provider-request authorization aligned when connection behavior changes.
- Keep the source verification proof on all source, favorites, and activity requests, and keep logical
412conflicts distinct from transport or instance failures. - Keep the initialization receipt and collection revision as the readiness boundary before publishing source changes to the application.
- Keep
SourceSyncService.changesconsumers aligned with source-list reloads, active-workspace hydration, and redirects after source removal. - Keep consented favorites, activity, and history runtime startup aligned with the selected source server and initialization order.
- Keep deletion markers and cleanup hooks aligned with source deletion and source ID reuse.
- Keep the one-time legacy activity migration restricted to recognized legacy instances and to source IDs already owned by the current profile.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The server instance ID changes | The client could write to a replacement database or send credentials to it | Verify the instance before protected requests and require reconnect on mismatch |
| A client writes with an old source revision | A newer source edit could be overwritten | Require If-Match for updates and surface a conflict on 412 |
| An operation uses the wrong route or conditional header | A create can overwrite an existing source or an inventory read can miss a change | Keep sourceRequestRoute and request-header rules aligned with the server API |
A network, gateway, invalid-JSON, or server 5xx response leaves the old verification proof active | A later request could send a token or provider credentials without a fresh instance check | Invalidate the proof for transport and server failures; keep ordinary 412 conflicts separate |
| An M3U URL changes and the replacement fetch fails | Channels from the old provider could remain under the new source | Clear the old projection and keep the source pending until a later retry succeeds |
| A delayed inventory read removes a changed local row | Local activity or a newer projection could be lost | Prune only when the observed revision still matches |
| A delayed device write uses an old row snapshot | New channels, EPG choices, or session changes could be reverted | Compare the snapshot before the write and merge only fields changed by the device |
| A Stalker connection changes while a session is active | Requests can use a token for the wrong portal or device | Invalidate session fields unless the session identity matches the accepted connection |
| A source changes or is deleted without retiring its source-associated context | A recreated source ID could inherit state from the old source | Retire the context on fingerprint change and deletion |
| The initialization receipt is not saved but the client publishes readiness | The app can expose an incomplete or falsely empty source set | Save the receipt before setting ready or publishing the change |
| Local deletion or cleanup fails | A deleted source can block a later projection or leave related state behind | Keep a durable cleanup marker and retry before projection |
| An optional favorites, activity, or history runtime fails during startup | Source synchronization can appear blocked or a related feature can remain unavailable | Start optional runtimes without making their failure reject source initialization |
Key files
| File | Why to read it |
|---|---|
| libs/…/source-sync.interface.ts | Defines the source, inventory, connection, and request contract |
| libs/…/source-definition.util.ts | Shows portable source conversion, validation, identity, and fingerprints |
| libs/…/source-api.ts | Shows how source operations become authorized browser or Electron server requests |
| libs/…/source-authorization.ts | Shows when instance verification permits or blocks protected requests |
| libs/…/source-sync.service.ts | Coordinates connection state, refresh, retries, and status changes |
| libs/…/synced-playlist-store.ts | Shows how server-owned definitions meet local playlist state and writes |
| apps/…/source-sync-runtime.ts | Connects synchronization changes and consented activity runtimes to the active application workspace |
| libs/…/source-materializer.ts | Explains how each source kind becomes a local usable playlist |
| libs/…/retain-source-projection.ts | Shows how delayed projections retain device activity and EPG choices |
| libs/…/source-row-snapshot.ts | Shows how delayed device writes avoid reverting newer local state |
| libs/…/legacy-playlist-activity.ts | Explains the one-time upgrade path for older source-sync profiles |
| libs/…/source-inventory-reconciliation.ts | Explains additions, updates, missing sources, and revision-safe pruning |
| libs/…/source-cleanup-queue.ts | Explains durable deletion and cleanup ownership |
| apps/…/sources-router.ts | Defines the self-hosted source API and server-side conflict rules |
| apps/…/source-sync.events.ts | Shows the Electron transport and server configuration boundary |
| apps/…/source-proxy.ts | Shows browser authorization for source-server and provider requests |