# Setting Bcrdt up - Blazor Server (InteractiveServer) One project, one DI container per circuit: `IBcrdtCollaborationService` is scoped, so every connected user gets their own replica and their own client id - never re-register it as a singleton, which would make every visitor one replica whose edits the engine cannot tell apart. The replicas of all circuits live in this one process, so they meet over `BcrdtInMemoryNetwork.Shared` with no relay at all; behind a load balancer, map `BcrdtHub` with a SignalR backplane and connect every circuit to it with `BcrdtSignalRTransportFactory` at an absolute address instead. Pages are prerendered first, and a document opened then belongs to a request that is already over: open it once `RendererInfo.IsInteractive`. A circuit outlives its browser tab by the circuit retention period, so `AddBitBcrdtCircuitPresence()` from `Bit.Bcrdt.Server` registers a `CircuitHandler` that suspends presence the moment the connection drops. ## Checklist 1. `dotnet add package Bit.Bcrdt` (the engine and the Blazor components) to the project with the collaborative components - the .Client project of a Blazor Web App - and a transport: `Bit.Bcrdt.SignalR`, or the WebSocket transport built into `Bit.Bcrdt`, or none at all for a purely local document. The relay is `Bit.Bcrdt.Server`, on the server. 2. Call `builder.Services.AddBitBcrdtServices(...)` in EVERY DI container that renders collaborative components - a Blazor Web App has two - through one shared extension method so both get the same configuration. It registers `IBcrdtCollaborationService` scoped: one replica per circuit in Blazor Server, one per app in WebAssembly. Never re-register it as a singleton. 3. Give it a transport with an ABSOLUTE address: the .NET SignalR client rejects `"/hub/bcrdt"` when the connection is built. Resolve it against the host - `builder.HostEnvironment.BaseAddress` in WebAssembly, `NavigationManager.BaseUri` where a component or a scoped service can ask. 4. On the relay: `builder.Services.AddBitBcrdtHub()` - not a bare `AddSignalR`, whose 32 KB receive limit disconnects every client once a document outgrows it - and `app.MapHub("/hub/bcrdt")`; or `app.UseWebSockets()` BEFORE `app.MapBitBcrdtWebSocket("/ws/bcrdt/{documentId}")`. Either relay only relays; add `builder.Services.AddBitBcrdtDocumentHost(...)` to keep every document on the server as well. The hub picks the host up from the container; the WebSocket mapping does not, so pass it in `options: new() { DocumentHost = app.Services.GetRequiredService() }`. 5. Add `@using Bit.Bcrdt` to `_Imports.razor`. 6. Open a document with ``, which opens it, cascades it and releases it - and waits until the page is interactive by itself - or with `IBcrdtCollaborationService.OpenAsync` paired with `CloseAsync`, and then only once the component is interactive wherever pages are prerendered: `RendererInfo.IsInteractive`. 7. Derive your own collaborative components from `BcrdtComponentBase` and `Track` what they render, so a change from anyone re-renders them, coalesced onto a frame. There is no script tag to add: `BcrdtTextArea` imports its small caret module from `_content/Bit.Bcrdt/bit-bcrdt.js` itself. ## The files ### `Program.cs` ```csharp using Bit.Bcrdt; var builder = WebApplication.CreateBuilder(args); builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); // One replica per circuit (the service is scoped), all in this process, meeting over the in-memory network. // The file store serializes the appends of every replica of a document, so they can share it. builder.Services.AddBitBcrdtServices(collaboration => collaboration .UseTransport(documentId => BcrdtInMemoryNetwork.Shared.CreateTransport(documentId)) .UseStore(new BcrdtFileSystemDocumentStore(Path.Combine(builder.Environment.ContentRootPath, "documents")))); // A circuit outlives its tab by the retention period, and would keep the user listed until then. This // registers a CircuitHandler - resolved from each circuit's own scope, so it sees that circuit's documents - // that withdraws them the moment the connection drops, and publishes them again if it comes back. builder.Services.AddBitBcrdtCircuitPresence(); var app = builder.Build(); app.UseAntiforgery(); app.MapStaticAssets(); app.MapRazorComponents() .AddInteractiveServerRenderMode(); app.Run(); ``` ### `Components/Pages/Notes.razor` ```razor @page "/notes/{Id}" @rendermode InteractiveServer @if (!RendererInfo.IsInteractive) {

Connecting…

} else { } @code { [Parameter] public string Id { get; set; } = default!; private readonly BcrdtCollaboratorInfo _me = new("u-1", "Ada", BcrdtPresenceService.SuggestColor("u-1")); } ``` ### `Components/_Imports.razor` ```razor @using Bit.Bcrdt ``` Behind a load balancer the circuits of one document are in different processes. Keep the same wiring, but map the hub (`AddBitBcrdtHub().AddStackExchangeRedis(...)` or another backplane, `MapHub`) and replace the in-memory transport with `new BcrdtSignalRTransportFactory("https://collab.example.com/hub/bcrdt")`. ## From the guide ### Hosting models The DI registration is identical in every hosting model, and `IBcrdtCollaborationService` is registered **scoped** for a specific reason: - **WebAssembly** - one scope per application, so scoped behaves as a singleton. Correct for a single-user client. - **Server** - one scope per circuit, so every connected user gets their own replica and their own client identity. Registering it as a singleton would make every visitor share one replica, and the engine could not tell their edits apart. - **Blazor Web App** - register it in both projects; whichever side renders interactively owns the replica. - **Hybrid (MAUI, WPF, WinForms)** - as WebAssembly, with the local process as the replica. `UseCrossTabSync()` acts on WebAssembly only and is a no-op elsewhere, so one registration still serves every project ([between tabs](#between-the-tabs-of-one-browser)). The [samples](Samples/) wire bit Bcrdt into each of these - their `Program.cs` (and the MAUI sample's `MauiProgram.cs`) is the registration to copy. The engine requires no JavaScript interop, so it runs unchanged in all of them, in a background service, in a console application, or on a server that hosts documents without rendering them. --- ### The five-minute version ```razor @using Bit.Bcrdt @code { private readonly BcrdtCollaboratorInfo _me = new("u-1", "Ada", BcrdtPresenceService.SuggestColor("u-1")); } ``` That is a working collaborative editor with live presence, automatic reconnection and offline support. The textarea also publishes its caret through presence, anchored to content rather than to an index, so an application can draw collaborators' cursors; nothing in the library draws them for you. There is no stylesheet to reference and no script tag to add: the one small module `BcrdtTextArea` needs (`_content/Bit.Bcrdt/bit-bcrdt.js`) is imported on demand. - `BcrdtCollaborationScope` opens the document, renders its content with the document as context (and cascades it), shows `LoadingContent` while it opens, follows `DocumentId` when it changes, and releases the document when it goes away. - `BcrdtSyncStatus` says where the cascaded document's connection stands - synced, syncing, reconnecting, offline, or local only when no transport is configured - with when it last synced while it is not, and "read-only" when the server granted read access only (see [Offline and reconnection](#offline-and-reconnection)). - `BcrdtComponentBase` - the base of all four components, and the base to derive your own collaborative components from - marshals change notifications onto the renderer and coalesces them onto a frame budget (`RenderInterval`, 16 ms by default), so a fast typist and five collaborators do not produce five hundred renders a second. Subscriptions made through its `Track` overloads are disposed with the component. ```razor @inherits BcrdtComponentBase

@_title.Count characters

@code { [CascadingParameter] public BcrdtCollaborativeDocument Document { get; set; } = default!; private BcrdtText _title = default!; protected override void OnParametersSet() { _title = Document.Text("title"); Track(_title); // re-render, coalesced, whenever the text changes - from anyone } } ``` Where a page is prerendered and then becomes interactive, a document must be opened only once the component is interactive (`RendererInfo.IsInteractive`) - a document opened while prerendering belongs to a request that is already over, and a browser store cannot be reached at all. `BcrdtCollaborationScope` does this by itself and shows `LoadingContent` while prerendering. Give it `ErrorContent` to show a document that cannot be opened instead of failing the page. `BcrdtTextArea` turns read-only - still showing everyone else's changes - when its `ReadOnly` parameter says so, or when the server granted the cascaded document's connection read-only access. If its small browser module cannot be loaded it is disabled rather than left accepting keystrokes that would reach nobody, and `OnError` says why. --- ### Presence Presence is **ephemeral, non-persistent, non-CRDT state** - who is here, where their cursor is, whether they are typing - deliberately separate from the document, with different guarantees. `BcrdtPresenceService` (`document.Presence`) is a typed façade over the awareness store: ```csharp document.Presence.SetUser(new BcrdtCollaboratorInfo("ada", "Ada", BcrdtPresenceService.SuggestColor("ada"))); document.Presence.SetCaret("body", document.Text("body"), 42); document.Presence.SetActivity("typing"); foreach (BcrdtCollaborator other in document.Presence.Others) { (int Start, int End)? selection = other.Selection?.Resolve(document.Crdt); // anchored to content, not to an index } ``` Each entry has a time-to-live (30 seconds by default, `BcrdtCollaborationOptions.PresenceTimeout`) refreshed by a heartbeat, so a crashed tab disappears from everyone's list without any server bookkeeping. `SuggestColor` hashes a user id into a fixed palette, so every participant agrees on everyone's colour with no coordination. In Blazor Server a circuit outlives its browser tab by the circuit retention period, and its documents keep publishing presence until then. To withdraw a user as soon as the connection drops, call `builder.Services.AddBitBcrdtCircuitPresence()` from `Bit.Bcrdt.Server`: it registers `BcrdtPresenceCircuitHandler`, which calls `BcrdtPresenceService.Suspend` on every document the circuit has open when the connection goes down, and `Resume` when it comes back. --- What a merge decides is the choice that matters most after the wiring - call `GetBcrdtGuideSection(heading: "Choosing a data type")`, and run concurrent edits through `InspectBcrdtMerge` before building on one. `GetBcrdtSourceFile(path: "Demo/Client/Pages/Live.razor")` is the page an application would write, and `GetBcrdtSourceFile()` lists the demo's working pages.