Kanject.Core.Realtime.Provider.AwsAppSync
1.3.0
Prefix Reserved
dotnet add package Kanject.Core.Realtime.Provider.AwsAppSync --version 1.3.0
NuGet\Install-Package Kanject.Core.Realtime.Provider.AwsAppSync -Version 1.3.0
<PackageReference Include="Kanject.Core.Realtime.Provider.AwsAppSync" Version="1.3.0" />
<PackageVersion Include="Kanject.Core.Realtime.Provider.AwsAppSync" Version="1.3.0" />
<PackageReference Include="Kanject.Core.Realtime.Provider.AwsAppSync" />
paket add Kanject.Core.Realtime.Provider.AwsAppSync --version 1.3.0
#r "nuget: Kanject.Core.Realtime.Provider.AwsAppSync, 1.3.0"
#:package Kanject.Core.Realtime.Provider.AwsAppSync@1.3.0
#addin nuget:?package=Kanject.Core.Realtime.Provider.AwsAppSync&version=1.3.0
#tool nuget:?package=Kanject.Core.Realtime.Provider.AwsAppSync&version=1.3.0
Kanject.Core.Realtime.Provider.AwsAppSync
Realtime push over AWS AppSync Events (Core proposal 0058 P3). UseAppSyncEvents() makes IRealtimePublisher one SigV4-signed POST /event per channel, and AWS fans out to the subscribers. Two adapters run your IRealtimeAuthorizer and Core's subscription rules inside the Event API's Lambda authorizer and a namespace's onSubscribe handler. The provider holds no state and references AWSSDK.Core only.
Installation
dotnet add package Kanject.Core.Realtime.Provider.AwsAppSync
Targets .NET 9, .NET 10 and .NET 11.
The host
builder.Services.AddRealtime(realtime =>
{
realtime.AuthorizeWith<ChatRealtimeAuthorizer>();
realtime.UseAppSyncEvents(options =>
{
options.HttpEndpoint = config["Realtime:EventApiHttpDomain"]!; // the Event API's Dns.Http
options.Namespace = "chat";
});
});
The provider never creates the Event API or the namespace. You hand it them, as proposal 0051 says. Credentials are a static pair only when both halves are set; otherwise the default chain, resolved on the first publish. A missing endpoint or namespace stops the host at startup (KANRT150).
The Event API
| Setting | Value |
|---|---|
| Connect and subscribe authorization | AWS_LAMBDA: a Lambda that calls AppSyncEventsHandlers.AuthorizeAsync |
| Publish authorization | AWS_IAM, which only your backend holds. Clients don't publish (OQ7), and the authorizer refuses EVENT_PUBLISH too |
The namespace's onSubscribe |
DIRECT behaviour, a Lambda data source that calls AppSyncEventsHandlers.OnSubscribeAsync |
Both entry points take and return the raw JSON, so pass the Lambda's input stream through: each has a Stream overload beside the bytes one, and neither involves a Lambda serializer (proposal 0067 D9). Resolve AppSyncEventsHandlers from a scope for each invocation:
public async Task<Stream> Authorize(Stream input, ILambdaContext context)
{
await using var scope = services.CreateAsyncScope();
return await scope.ServiceProvider.GetRequiredService<AppSyncEventsHandlers>().AuthorizeAsync(input);
}
Under Native AOT, start the runtime loop in a Main of your own, as the API Gateway provider's README shows: Lambda Annotations' generated Main doesn't compile for a Stream-in, Stream-out function (Annotations 2.4.2).
The authorizer returns the principal as the handler context. It also refuses a subscribe that breaks Core's rules, because AppSync admits a wildcard on any namespace that has no onSubscribe handler (P0 (b)). onSubscribe refuses an event that carries no handler context, applies Core's rules, then asks your CanSubscribeAsync about a topic. It returns null to accept and {"error":…} to refuse.
Channels
| Audience | Channel |
|---|---|
RealtimeAudience.User(id) |
/{namespace}/users/{id} |
RealtimeAudience.Users(ids) |
One publish per user channel, at most 64 at once (MaxConcurrentPublishes) |
RealtimeAudience.Topic(kind, id) |
/{namespace}/{Kind}/{id} |
A client subscribes to its own user channel and to the topics it has open, up to 200 per connection.
In a tenant (0058 OQ19), every channel carries the tenant's segment after the namespace:
/{namespace}/{segment}/users/{id} and /{namespace}/{segment}/{Kind}/{id}. The segment is a name-based Guid of the
tenant id (proposal 0065), so any tenant id fits AppSync's channel rules; the client reads it from your backend, which
gets it from RealtimeTenants.SegmentOf(tenantId). The tenant itself comes from the principal your authorizer returns,
and travels in the handler context (tenantId). A subscribe to any channel but the principal's own tenant's is refused
as AnotherTenant, including a channel of no tenant. A publish goes to the channels of the scope's
ITenantContext tenant, or to the channels of no tenant when there is none.
What it can't do
- It can't list, end or unsubscribe a connection. AppSync has no API for any of them (P0 (h)).
IRealtimeConnectionsandIRealtimeSubscriptionsthrow KANRT004, andSyncAsynchas nothing to sync. - An expired or signed-out user keeps receiving until the socket closes, up to 24 hours. AppSync never re-authorizes a live subscription (P0 (j)), so
EndsExpiredConnectionsis no. Choose API Gateway if you must cut a device at once. - Publishes are metered per delivery. A publish to N subscribers is about N + 1 operations (P0 (f)).
The client
AppSync's Events WebSocket protocol, with these duties:
- Drop repeats. Drop a repeated envelope
id: one connection subscribed to two matching channels gets an event once per subscription. - Drop your own echo. Drop or reconcile an envelope whose
requestIdyou minted. - Read
data.eventas one JSON string. It arrives as a single string, and AppSync may re-serialize it. Compare values, never bytes. - Retry a throttled authorizer. A
connection_errorwithAuthorizerFailureExceptionmeans the Lambda authorizer was throttled, so reconnect.
Diagnostics
| Code | Meaning |
|---|---|
| KANRT150 | A setting is missing or malformed |
| KANRT151 | The Event API refused a publish |
| KANRT152 | The Event API reported the event failed |
License
Licensed under the Kanject Code Libraries License Agreement (KCLLA); the full text ships in this package as LICENSE.md. Organizations whose trailing-twelve-month gross revenue and total funding raised are each below US$250,000 may use it at no cost under the Free Tier. At or above either threshold a commercial license is required — contact commercial@kanjectbusiness.com.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net11.0 is compatible. |
-
net10.0
- AWSSDK.Core (>= 4.1.1.2)
- Kanject.Core.Realtime (>= 1.3.0)
-
net11.0
- AWSSDK.Core (>= 4.1.1.2)
- Kanject.Core.Realtime (>= 1.3.0)
-
net9.0
- AWSSDK.Core (>= 4.1.1.2)
- Kanject.Core.Realtime (>= 1.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
- Takes Kanject.Core.Realtime 1.3.0.