Skip to content

PulseSharp.Sync.Server ​

Device to cloud sync: the ASP.NET Core endpoint on the server.

Generated from the XML documentation of PulseSharp.Sync.Server.

PulseSharpSyncEndpointExtensions ​

csharp
public static class PulseSharpSyncEndpointExtensions

Maps the endpoint of PulseSharp sync in an ASP.NET Core app.

MapPulseSharpSync(IEndpointRouteBuilder, string) ​

Maps the sync endpoint that devices call. Add the sync server first with PulseSharpSyncServerExtensions.UseSyncServer.

ParameterDescription
endpointsThe app's endpoints.
patternThe address of the endpoint. The default is /pulse/sync.

Returns: A builder for the endpoint, for example to add .RequireAuthorization().

Throws InvalidOperationException when the sync server was not added, or the store can't sync.

PulseSharpSyncServerExtensions ​

csharp
public static class PulseSharpSyncServerExtensions

Adds the server side of PulseSharp sync.

UseSyncServer(PulseSharpBuilder, Action<SyncServerOptions>) ​

Lets devices sync with this app: they send the events they emit, and receive this app's events. Map the endpoint with app.MapPulseSharpSync().

ParameterDescription
pulseThe PulseSharp builder.
configureSays which nodes may sync and what they may receive.

Returns: The builder.

Throws InvalidOperationException when no node is allowed, or the sync server is added twice.

Example

csharp
builder.Services.AddPulseSharp(pulse =>
{
    pulse.UsePostgreSql(connectionString);
    pulse.Node = "cloud";
    pulse.UseSyncServer(server => server.AllowNode("till-1", tillKey).PushStreams(node => [$"Sale-{node}-"]));
});

app.MapPulseSharpSync();

SyncServerOptions ​

csharp
public sealed class SyncServerOptions

Who may sync with this server and what they may receive. Set them in pulse.UseSyncServer(server => { ... }).

Every request must be from a known node: one with a key from SyncServerOptions.AllowNode, or a signed-in user that SyncServerOptions.NodeFrom turns into a node name. A node may only send events it emitted itself. By default it receives every event it did not emit; SyncServerOptions.AllowNode and SyncServerOptions.PullStreams narrow that down.

MaxBatchSize ​

The most events one request may carry. The default is 1000.

AllowNode(string, string, string[]) ​

Lets a node sync with a key, for example a till that sets sync.ApiKey in pulse.UseSync(...).

ParameterDescription
nodeThe node's name: the pulse.Node of that app.
apiKeyThe node's secret key. Keep it out of source code, for example in configuration.
pullStreamsThe stream name prefixes this node may receive, for example Product-. None means every event it did not emit.

Returns: These options.

Example

csharp
server.AllowNode("till-1", configuration["Tills:till-1"]!, "Product-", "Price-");

NodeFrom(Func<ClaimsPrincipal, string>) ​

Lets signed-in users sync, for apps that already authenticate devices (for example with tokens).

ParameterDescription
nodeReturns the node name of a signed-in user, or null when the user may not sync.

Returns: These options.

Example

csharp
server.NodeFrom(user => user.FindFirst("device")?.Value);

PullStreams(Func<string, IReadOnlyList<string>>) ​

Limits what each node may receive, by stream name prefix. Used for nodes that SyncServerOptions.AllowNode gave no prefixes.

ParameterDescription
allowedStreamsReturns the prefixes a node may receive. An empty list means nothing.

Returns: These options.

Example

csharp
server.PullStreams(node => [$"Store-{StoreOf(node)}-", "Product-"]);

PushStreams(Func<string, IReadOnlyList<string>>) ​

Says which streams each device may write. A device that sends an event for any other stream has that event refused (the server logs a warning and the device logs one too); its other events are saved as usual. This keeps a device from writing into streams that belong to the server or to another device. The names are prefixes.

ParameterDescription
allowedStreamsGets a node's name and returns the stream name prefixes it may write.

Returns: These options.

Example

csharp
server.PushStreams(node => [$"Sale-{node}-"]);

PushAnyStream() ​

Lets every device write every stream. Only choose this when you trust all devices completely: a device could then write into streams that belong to the server, for example a price list.

Returns: These options.

Released under the MIT License.