PhoenixKitSync.WebSocketClient (PhoenixKitSync v0.3.0)

Copy Markdown View Source

WebSocket client for Sync receiver connections.

Uses WebSockex to connect from the receiver site to the sender's WebSocket endpoint. Sends requests and receives responses.

Architecture

The receiver uses this client to:

  1. Connect to the sender's hosted channel
  2. Send requests for data (tables, schema, records)
  3. Receive responses and notify the caller (LiveView)

Usage

{:ok, pid} = WebSocketClient.start_link(
  url: "https://sender-site.com",
  code: "ABC12345",
  caller: self()
)

# Request available tables
WebSocketClient.request_tables(pid)
# Receive: {:sync_client, {:tables, tables}}

# Request records
WebSocketClient.request_records(pid, "users", offset: 0, limit: 100)
# Receive: {:sync_client, {:records, "users", result}}

Rate limiting

A sender that refuses a request with "error_code": "rate_limited" (the connection is over its requests per minute) gets the same request again after its "retry_after", clamped and within a total budget (PhoenixKitSync.RateLimit). Before each wait the caller receives {:sync_client, {:rate_limited, request_type, seconds}}; past the budget the request ends as {:request_error, request_type, :rate_limited}.

Summary

Functions

Disconnects the WebSocket client.

Request server capabilities.

Request record count for a table.

Request records from a table with pagination.

Request schema for a specific table.

Request list of available tables from sender.

Starts the WebSocket client and connects to the sender.

Functions

disconnect(pid)

@spec disconnect(pid()) :: :ok

Disconnects the WebSocket client.

request_capabilities(pid)

@spec request_capabilities(pid()) :: :ok

Request server capabilities.

request_count(pid, table)

@spec request_count(pid(), String.t()) :: :ok

Request record count for a table.

request_records(pid, table, opts \\ [])

@spec request_records(pid(), String.t(), keyword()) :: :ok

Request records from a table with pagination.

Options

  • :offset - Number of records to skip (default: 0)
  • :limit - Maximum records to return (default: 100)
  • :after - The next_after of the page before, when the sender answered one: the sender then continues from that key and ignores the offset. A sender that does not know it pages by the offset, so both are sent.

request_schema(pid, table)

@spec request_schema(pid(), String.t()) :: :ok

Request schema for a specific table.

request_tables(pid)

@spec request_tables(pid()) :: :ok

Request list of available tables from sender.

start_link(opts)

@spec start_link(keyword()) :: {:ok, pid()} | {:error, any()}

Starts the WebSocket client and connects to the sender.