Skip to content

Live, across tabs

Every other page in this demo keeps its replicas side by side in one tab, talking over the library's in-memory transport. This one is the real thing: one replica per browser tab, meeting through the demo server. Open the link below in another tab, another browser or on your phone and type in both.

One replica per tab

This tab is one replica. Share the link, and every tab that opens it is another; a tab opened from the read-only link follows every change but cannot make one.

Connecting…

The thing worth trying. Take one tab offline, type a paragraph into it, type something else into another tab, then bring the first one back. Both paragraphs survive, in the same order in every tab - the handshake sends each side what the other is missing, in both directions. Reload the offline tab before bringing it back, or close every tab and open the link again: a browser that saved a copy picks up where it left off. The demo server keeps one more copy, but only in memory - a restart or newer rooms evict it - so it is a stopgap, not an archive. Then open the read-only link: it follows every edit, and makes none.

The page an application would write

The document comes from the app's own AddBitBcrdtServices registration and is opened by BcrdtCollaborationScope, which shows LoadingContent while the page is prerendered and the document opens, ErrorContent if it cannot be opened, and closes it when the page goes away. The editor is BcrdtTextArea and the list of people is BcrdtPresenceView. Each tab keeps its copy in the browser's IndexedDB (UseIndexedDbStore), so a reload - online or not - starts from where it left off.

On the server, BcrdtHub from Bit.Bcrdt.Server relays each message to the other tabs, and a BcrdtDocumentHost behind it is one more peer in every room: it loads a document from its store when the first tab arrives, answers each joining tab with the whole document, writes every change to the store, and unloads the document UnloadDelay after the last tab leaves. A newcomer to an empty room still gets everything that was ever typed into it, and server code can read and change a hosted document too (ReadAsync, EditAsync).

Two relays

The demo server maps both relays Bit.Bcrdt.Server ships: BcrdtHub for the SignalR transport of Bit.Bcrdt.SignalR, and the WebSocket endpoint for the WebSocket transport built into Bit.Bcrdt. Tabs only meet on the same relay, so the relay is part of the link you share.

// Demo/Server/Program.cs
builder.Services.AddBitBcrdtHub();   // AddSignalR, with a receive limit sized for sync messages
builder.Services.AddBitBcrdtDocumentHost(_ => new BcrdtDocumentHostOptions
{
    Store = new BcrdtInMemoryDocumentStore(maxDocuments: 500),   // an app gives it a durable store
    UnloadDelay = TimeSpan.FromMinutes(1),
});
// ...
app.UseWebSockets();

app.MapHub<LiveHub>("/hub/bcrdt");   // a BcrdtHub; picks the host up from the container
app.MapBitBcrdtWebSocket("/ws/bcrdt/{documentId}", options: new()
{
    DocumentHost = app.Services.GetRequiredService<BcrdtDocumentHost>(),
    AuthorizeAccess = (context, documentId) => ValueTask.FromResult(LiveHub.AccessOf(context)),
});

Who may edit

A CRDT makes replicas converge; it says nothing about who may change what. So the server decides, per connection and document, by overriding BcrdtHub.AuthorizeAccessAsync (or setting the WebSocket endpoint's AuthorizeAccess): None refuses the join, ReadOnly lets the tab follow the document and publish presence while the relay drops every change it sends, ReadWrite lets it edit. The demo's LiveHub grants read-only to a link that asks for it; an application decides from the signed-in user. The client is told its grant on joining (document.IsReadOnly), and BcrdtTextArea stops taking input by itself. Rules about what a writer may change go in BcrdtDocumentHostOptions.ValidateUpdate, which sees every change before anyone else receives it.

An unhandled error has occurred. Reload 🗙