% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/client.R
\name{sync_fetch}
\alias{sync_fetch}
\title{Fetch a document from a sync server}
\usage{
sync_fetch(
  url,
  doc_id,
  timeout = 5000L,
  tls = NULL,
  token = NULL,
  verbose = FALSE
)
}
\arguments{
\item{url}{WebSocket URL of the sync server (e.g., "ws://localhost:3030/"
or "wss://sync.automerge.org/"). Note: trailing slash may be required.}

\item{doc_id}{Document ID (base58check encoded string)}

\item{timeout}{Timeout in milliseconds for each receive operation. Default 5000.}

\item{tls}{(optional) for secure wss:// connections to servers with
self-signed or custom CA certificates, a TLS configuration object
created by \code{\link[nanonext:tls_config]{nanonext::tls_config()}}.}

\item{token}{(optional) JWT (ID token) for authenticated servers.
Sent as a Bearer token in the Authorization header of the WebSocket
upgrade request.}

\item{verbose}{Logical, print debug messages. Default FALSE.}
}
\value{
An automerge document object containing the fetched data.
}
\description{
Connects to an automerge-repo sync server and retrieves a document by ID.
This is useful for debugging sync issues or fetching documents from remote
servers like sync.automerge.org.
}
\details{
The function implements the automerge-repo sync protocol:
\enumerate{
\item Connects via WebSocket
\item Sends join message with peer ID
\item Receives peer response from server
\item Sends sync request for the specified document
\item Receives and applies sync messages until complete
}

Sync is considered complete when no new messages arrive within the timeout
after at least one sync round.
}
\examples{
\dontshow{if (interactive()) withAutoprint(\{ # examplesIf}
# Fetch from public sync server
doc <- sync_fetch("wss://sync.automerge.org", "4F63WJPDzbHkkfKa66h1Qrr1sC5U")

# Fetch from local server with debug output
doc <- sync_fetch(server$url, "myDocId", verbose = TRUE)

# Fetch from server with self-signed certificate
cert <- nanonext::write_cert()
tls <- nanonext::tls_config(client = cert$client)
doc <- sync_fetch(server$url, "myDocId", tls = tls)

# Fetch from authenticated server
doc <- sync_fetch(
  "wss://secure.example.com",
  "myDocId",
  token = "eyJhbGciOi..."
)

# Inspect the document
automerge::am_keys(doc)
\dontshow{\}) # examplesIf}
}
