Skip to content
Shiny PlatformShiny Platform
.NET MAUI on the WebRun your MAUI app in the browser on WebAssembly - real DOM elements, no Blazor. Try the web sample and the .NET MAUI GalleryGet started

Mesh

Shiny.Actors.Mesh serves your actors to other devices as a Universal Hubs hub. Phones reach each other over whichever transport works: Bluetooth LE, Wi-Fi, Apple’s MultipeerConnectivity or Google’s Nearby Connections. That includes planes, parks and guest Wi-Fi that blocks device-to-device traffic. Calls, streams, errors and request context behave exactly as they do over HTTP, and the client is the same RemoteActorSystem.

Terminal window
dotnet add package Shiny.Actors.Mesh
dotnet add package Shiny.UniversalHubs.BluetoothLE # and/or .Http, .Multipeer, .Nearby

Two registrations. UseMesh on the actor builder says what is served and who may call it. AddActorMesh on the universal server adds the hub and picks its transports.

builder.Services.AddShinyActors(x => x
.UseDocumentDb(sqlite)
.AddDurableStream<ChatMessage>()
.UseMesh(mesh => mesh
.Expose<IChatRoom>() // nothing is reachable unless exposed
.ExposeStream<ChatMessage>()
.RequireSharedKey(meshKey) // how connections authenticate - required
.MapRequestContext("device", user => user.Identity?.Name))); // a server-owned context key
builder.Services.AddUniversalServer(server => server
.Configure(o => o.LocalName = DeviceInfo.Current.Name)
.Nearby("com.mycompany.chat")
.AddActorMesh(hub => hub
.UseHttp("_chat._tcp")
.UseBluetoothLE("180D", "6e400002-b5a3-f393-e0a9-e50e24dcca9e")
.UseNearby()));
await services.GetRequiredService<IUniversalHubHost>().Start();

AddActorMesh is a hub like any other on the server, so an app can host its own hubs beside it. Pass a name as the second argument if shiny-actors (ActorMesh.HubName) doesn’t suit. Platform setup (Info.plist, Android permissions) is the same as for any Universal Hubs host.

builder.Services.AddUniversalHubClient<IActorMeshHub>(ActorMesh.HubName, o => o
.UseHttp("_chat._tcp")
.UseBluetoothLE("180D", "6e400002-b5a3-f393-e0a9-e50e24dcca9e")
.UseNearby("com.mycompany.chat"));
var client = services.GetRequiredService<IUniversalHubClient<IActorMeshHub>>();
using var scan = client.DiscoverAll().Subscribe(host => hosts.Add(host)); // one list across every transport
await client.Connect(host, new HubConnectOptions("Allan's phone"));
var actors = client.CreateActorSystem(new SharedKeyCredentials(meshKey, "allans-phone"));
await actors.Get<IChatRoom>("lobby").Post("hi");
await foreach (var e in actors.GetStream<ChatMessage>("lobby").ReadFromAsync(lastSeen, ct))
lastSeen = e.Sequence; // a dropped link picks up from here, on every transport

CreateActorSystem wraps the hub client in an ActorMeshTransport. It authenticates before the first call, and again if the host stops recognising the connection, for example after a drop that couldn’t be resumed. Connecting the hub client stays your job. Call transport.AuthenticateAsync() to find out about a wrong key straight away instead of on the first call.

Anyone in radio range can connect over Bluetooth, Multipeer or Nearby, so a mesh host must say how connections authenticate. Registration throws until it does:

RequireSharedKey(key) Every device holds the same secret. It proves it holds the key without sending it.
Authenticate(IActorMeshAuthenticator) A scheme of your own: a token from your server, a pairing record.
AllowAnonymous() Anyone who can reach the hub. For trusted networks, demos and tests.

Each connection goes through a challenge before anything else:

  1. The client asks for a challenge: a single-use nonce, the host id and its own connection id.
  2. It answers. With a shared key, the answer is an HMAC-SHA256 over both sides’ nonces, the host id, the connection id and the identity it claims. The answer is useless on another connection, against another host, or a second time.
  3. The host checks the answer in constant time. A wrong answer, or an answer to a spent nonce, closes the connection.
  4. With a shared key, the host proves the key back. The client disconnects from a host that can’t, so a device that merely claims to be in the mesh never receives a call.

Until a connection authenticates, every call gets 401. A connection that hasn’t authenticated within AuthenticationTimeout (15 seconds) is closed, so a stranger can’t hold one of the hub’s MaxClients slots. A resumed link stays authenticated.

var meshKey = SharedKeyAuthenticator.GenerateKey(); // 32 random bytes - hand it out by QR code, pairing, your server

A custom scheme implements IActorMeshAuthenticator on the host and IActorMeshCredentials on the client. The host gets the nonce and the client’s MeshCredentials and returns a ClaimsPrincipal, or null to refuse.

Authorization works the same way as over HTTP. The callbacks see the authenticated User and the Caller.Connection, which carries the transport, the client’s name and its properties:

mesh.Expose<IWallet>(call => call.ActorId == call.User.Identity?.Name)
.ExposeStream<OrderPlaced>(s => s.Access == ActorStreamAccess.Subscribe || s.Key == s.User.Identity?.Name)
.MapRequestContext((caller, context) => context["transport"] = caller.Connection.Transport?.DisplayName ?? "");

A refused call or stream fails with RemoteActorException 403. The caller’s own ActorRequestContext values cross the wire, but a key made server-owned with MapRequestContext(key, ...) is always set from the authenticated identity.

Every call and stream travels through one hub, IActorMeshHub, so a device serves one hub for all of its actors:

Hub method
Challenge / Authenticate The handshake above.
Invoke(MeshCall) Contract, actor id, method, the JSON argument array, request context and the call chain. Returns a status and a JSON body.
Publish(stream, key, item) Status 202, or a refusal.
Subscribe(stream, key, afterSequence, requireDurable) An IAsyncEnumerable of events with their sequence. A durable stream replays first.

Values stay raw JSON from end to end, so nothing is serialized twice. The call chain crosses too, which means A -> B on another device -> A fails with ActorDeadlockException instead of hanging. Status codes match HTTP remoting: 400 bad arguments, 401/403 auth, 404 not exposed, 409 deadlock, 500 the actor threw.