% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/server.R
\name{sync_server}
\alias{sync_server}
\title{Create an Automerge sync server}
\usage{
sync_server(
  port = 0L,
  host = "127.0.0.1",
  data_dir = tempfile("autosync"),
  auto_create_docs = TRUE,
  storage_id = NULL,
  tls = NULL,
  auth = NULL,
  share = NA
)
}
\arguments{
\item{port}{Port to listen on. Default 0 (binds to a random available port).
The actual URL is retrieved via \code{server$url}.}

\item{host}{Host address to bind to. Default "127.0.0.1" (localhost).}

\item{data_dir}{Directory for document storage. Defaults to a
session-temporary directory, so documents do not persist across R
sessions. Supply an explicit path to persist documents across sessions.}

\item{auto_create_docs}{Logical, whether to auto-create documents when
clients request unknown document IDs. Default TRUE.}

\item{storage_id}{Optional storage ID for this server. If NULL (default),
generates a new persistent identity. Set to NA for an ephemeral server
(no persistence identity).}

\item{tls}{(optional) for secure wss:// connections, a TLS configuration
object created by \code{\link[nanonext:tls_config]{nanonext::tls_config()}}.}

\item{auth}{Optional authentication configuration created by \code{\link[=auth_config]{auth_config()}}.
When provided, clients must include a valid JWT (ID token) as a
Bearer token in the Authorization header of the WebSocket upgrade request.
Connections without valid credentials are rejected immediately.
Note: TLS is required when authentication is enabled to protect tokens.}

\item{share}{Controls document sharing policy for connected clients. This
unified parameter governs both proactive document announcement (pushing
documents to clients) and access control (allowing clients to request
documents). Accepts one of:
\itemize{
\item \code{NA} (default) — never announce but allow all requests.
\item \code{TRUE} — announce all documents to all clients and allow all
requests.
\item \code{FALSE} — never announce and deny all requests (sends
\code{doc-unavailable}).
\item A function with signature \verb{function(client_id, doc_id)}
returning \code{TRUE} (announce and allow), \code{NA} (allow on request only),
or \code{FALSE} (deny access). Called per client and per document.
}}
}
\value{
An autosync_server object inheriting from 'nanoServer', with
\verb{$start()} and \verb{$close()} methods.
}
\description{
Creates a WebSocket server that implements the automerge-repo sync protocol,
compatible with JavaScript, Rust, and other Automerge clients.
}
\details{
The returned server inherits from nanonext's nanoServer class and provides
\verb{$start()} and \verb{$close()} methods for non-blocking operation.
}
\examples{
\dontshow{if (interactive()) withAutoprint(\{ # examplesIf}
# Create and start a server
server <- sync_server()
server$start()

# Server is now running in the background
# ...do other work...

# Stop when done
server$close()

# With TLS for secure connections
cert <- nanonext::write_cert()
tls <- nanonext::tls_config(server = cert$server)
server <- sync_server(tls = tls)
server$start()
server$url
server$close()

# Server with OIDC authentication (requires TLS)
cert <- nanonext::write_cert()
tls <- nanonext::tls_config(server = cert$server)
server <- sync_server(
  tls = tls,
  auth = auth_config(
    client_id = "123456789.apps.googleusercontent.com",
    allowed_domains = "mycompany.com"
  )
)
\dontshow{\}) # examplesIf}
}
