Real-time collaboration
Keep the document shown in a .NET MAUI PdfView in step with other copies through NexPDF Sync, with PdfView.CreateSync and the NexPdfSync client for pages, annotations, comments, form values and bookmarks.
License module
Sync needs the annotations module (Indie tier and above).
NexPDF Sync lets several copies of one PDF (the browser viewer, apps, a server) edit the same
document at the same time and converge. Copies exchange records (page, annotation,
comment, field values, bookmark items and shared objects such as appearance streams and
fonts) with stable ids, not whole files. Edits made with any other call are picked up
automatically, and the sync state is saved in the PDF.
Connect a PdfView
view.CreateSync(options) returns a NexPdfSync client (the client of the
.NET SDK) for the document the view shows. It
shares the view's engine lock, so rendering and sync never touch the document at the same time,
raises its events on the calling thread's SynchronizationContext (the UI thread when created
there), re-renders the view after remote changes, and reopens the server's document on Reload.
using Nexdevit.NexPdf;
using Nexdevit.NexPdf.Maui;
var options = new NexPdfSyncOptions("https://viewer.example.com/s/<session id>")
{
Name = "Ana",
Color = "#E5484D",
Token = token,
};
using (var first = new NexPdfSync(options)) // start from the server's copy
{
await view.LoadAsync(await first.DownloadDocumentAsync());
options.Since = first.Seq;
}
var live = view.CreateSync(options); // load a document first
live.Presence += (_, peers) => ShowCursors(peers); // NexPdfSyncPeer: Name, Color, Page, Cursor
live.StatusChanged += (_, status) => StatusLabel.Text = status.ToString();
await live.ConnectAsync(); // completes on hello
await live.EngineLock.WaitAsync(); // the view's lock: hold it while editing
try { view.Document!.AddAnnotation(0, "Square", "#FF0000", 1, "note", 50, 50, 150, 120); }
finally { live.EngineLock.Release(); }
await view.ReloadAsync();
await live.FlushAsync(); // or let the 1 s timer push it
await live.SetPresenceAsync(view.CurrentPage, (100, 200));The client connects over a WebSocket first and falls back to Server-Sent Events + POST for the
session when the socket never reaches hello. It applies the others' changes, skipping its own,
pushes local edits every second (FlushInterval) or on FlushAsync(), and reconnects with a
backoff from 1 s to 30 s, but never after 401, 402, 403 or 404. Transport
(NexPdfSyncTransport.Auto, WebSocket or Sse) forces one transport.
Call live.Close() (or Dispose()) when the page goes away; call FlushAsync() first to send
pending edits. The options, events (Remote, Pushed, Conflict, Presence, StatusChanged,
Reload, Error) and properties are listed in the
.NET collaboration guide.
Exchange changes yourself
The sync methods on view.Document exchange change sets without the client, for a transport of
your own:
using System.Text.Json.Nodes;
var doc = view.Document!;
doc.SyncSetClientId(clientId); // 1 to 64 characters of A-Z a-z 0-9 - _ . : @
ulong lastClock = 0;
var outgoing = doc.SyncExportLocalJson(lastClock); // this copy's own changes
lastClock = JsonNode.Parse(outgoing)!["clock"]!.GetValue<ulong>();
var report = JsonNode.Parse(doc.SyncApplyJson(incoming))!; // changes from the others
await view.ReloadAsync(); // show what was applied| Call | Returns |
|---|---|
SyncSetClientId(id) | Sets the id this copy versions its changes under. Random when not set |
SyncExportLocalJson(since) | Only this copy's own changes with a clock above since; keep the returned clock |
SyncExportJson(since) | Every change above since, from any client |
SyncApplyJson(changes) | Applies a change set and returns a report |
SyncSnapshotJson() | Every record with its version |
Every copy must start from the same file.
How changes merge
Change sets use the format nexpdf-sync/2 (format 1 is still read). Every property of a record
has its own version {"lamport", "client"}, and the merge is last writer wins per property (a
higher Lamport clock, then the higher client id), so every copy picks the same result in whatever
order changes arrive. Alice changing an annotation's text while Bob moves it keeps both edits.
Page operations (insert, delete, move, rotate, crop, content edits) merge with a deterministic page
order. The apply report has ok, clock, applied, ignored, conflicts (informational, with
the props in conflict) and errors.
The sync server
The NexPDF web viewer server hosts the sync endpoints: GET /api/sync/ws (WebSocket),
GET /api/sync/document, GET /api/sync/stream (Server-Sent Events), POST /api/sync/push,
POST /api/sync/presence, GET /api/sync/changes?since=<seq> and GET /api/sync/snapshot. It
keeps the accepted changes on disk, so a restarted server continues where it left off. The
endpoints, record format and storage are described in the
.NET collaboration guide.
Limits
- Attachments are not records. Replacing the document on the server sends
reload, whichCreateSynchandles by reloading the view.