Handles cross-site notification when creating sender connections.
When a sender connection is created, this module notifies the remote site so they can automatically register the incoming connection on their end.
How It Works
- When you create a sender connection pointing to a remote site (e.g., "https://remote.com")
- This module calls
POST https://remote.com/{prefix}/db-sync/api/register-connection - The remote site creates a receiver connection automatically
- The result is recorded in the connection's metadata
Remote Site Responses
- 200 OK - Connection registered successfully
- 401 Unauthorized - Password required or invalid
- 403 Forbidden - Incoming connections denied
- 409 Conflict - Connection already exists
- 503 Service Unavailable - DB Sync module disabled
Usage
Usually called automatically when creating connections via the LiveView UI. Can also be called manually:
{:ok, result} = ConnectionNotifier.notify_remote_site(connection, token, password: "optional")
Summary
Functions
Checks the status of a remote site's DB Sync API.
Fetches the list of available tables from the sender.
Fetch table records from a sender site via HTTP API for preview.
Fetch table schema from a sender site via HTTP API.
Notifies a remote site to delete a connection.
Notifies a remote site about a new sender connection.
Notifies a remote site of a status change (suspend, reactivate, revoke).
Pulls data for a specific table from the sender.
Queries the sender for the current connection status.
Verifies a connection still exists on the remote site.
Types
Functions
Checks the status of a remote site's DB Sync API.
Parameters
site_url- The remote site's base URL
Returns
{:ok, status}- Remote site status{:error, reason}- Failed to contact site
Fetches the list of available tables from the sender.
Called by receiver to get a list of tables that can be synced.
Parameters
connection- The receiver connection (must have site_url and auth_token/auth_token_hash)opts- Options::timeout- HTTP request timeout (default: 30_000ms)
Returns
{:ok, tables}- List of table info maps with :name, :row_count, :size_bytes{:error, :offline}- Sender is offline{:error, reason}- Failed to fetch
Fetch table records from a sender site via HTTP API for preview.
Options:
:limit- Maximum number of records to fetch (default: 10):offset- Offset for pagination (default: 0):ids- List of specific IDs to fetch:id_range- Tuple of {start_id, end_id}
Returns:
{:ok, records}- List of record maps{:error, :offline}- Sender is offline{:error, reason}- Failed to fetch records
Fetch table schema from a sender site via HTTP API.
Returns:
{:ok, schema}- Map with :columns list{:error, :offline}- Sender is offline{:error, reason}- Failed to fetch schema
Notifies a remote site to delete a connection.
Called when a receiver deletes their connection - notifies the sender to also delete.
Parameters
connection- The connection being deleted (must have site_url and auth_token_hash)opts- Options::timeout- HTTP request timeout (default: 30_000ms)
Returns
{:ok, :deleted}- Remote site deleted the connection{:ok, :not_found}- Connection didn't exist on remote (already deleted){:ok, :offline}- Remote site is offline (will self-heal later){:error, reason}- Failed to notify
@spec notify_remote_site(map(), String.t(), keyword()) :: {:ok, notify_result()} | {:error, any()}
Notifies a remote site about a new sender connection.
Parameters
connection- The sender connection that was just createdraw_token- The raw auth token (only available at creation time)opts- Options::password- Password to provide to remote site (if required):timeout- HTTP request timeout (default: 30_000ms)
Returns
{:ok, result}- Notification sent, result contains details{:error, reason}- Failed to send notification
Notifies a remote site of a status change (suspend, reactivate, revoke).
Called when a sender changes their connection status - the receiver should mirror it.
Parameters
connection- The connection with updated statusnew_status- The new status ("active", "suspended", "revoked")opts- Options::timeout- HTTP request timeout (default: 30_000ms)
Returns
{:ok, :updated}- Remote site updated the status{:ok, :not_found}- Connection not found on remote{:ok, :offline}- Remote site is offline{:error, reason}- Failed to notify
Pulls data for a specific table from the sender.
Called by receiver to fetch table data during sync.
Parameters
connection- The receiver connectiontable_name- Name of the table to pullopts- Options::timeout- HTTP request timeout (default: 60_000ms for large data):conflict_strategy- How to handle existing records ("skip", "overwrite", "merge")
Returns
{:ok, result}- Map with :records_imported, :records_skipped, etc.{:error, :offline}- Sender is offline{:error, reason}- Failed to pull
Queries the sender for the current connection status.
Called by receiver to sync their status with the sender's status.
Parameters
connection- The receiver connection (must have site_url and auth_token_hash)opts- Options::timeout- HTTP request timeout (default: 30_000ms)
Returns
{:ok, status}- Current status from sender ("active", "suspended", "revoked"){:ok, :offline}- Sender is offline{:ok, :not_found}- Connection not found on sender{:error, reason}- Failed to query
Verifies a connection still exists on the remote site.
Called by sender to check if receiver still has the connection. If not, the sender should delete their own connection.
Returns
{:ok, :exists}- Connection exists on remote{:ok, :not_found}- Connection was deleted on remote{:ok, :offline}- Remote site is offline{:error, reason}- Failed to verify