PouryaKit.Realtime
Server-sent events pushed from Notification Handlers.
Server-sent events pushed from Notification Handlers. A handler publishes a plain record on a Channel, and the push is a PostgreSQL notification written on the delivery's own transaction, so every instance of the Host hears it only when that delivery commits. Each instance passes it on to its connected clients of the delivery's tenant that listen on the Channel.
Install
dotnet add package PouryaKit.Realtime --prereleaseWhen to use it
- Browsers should see a change as soon as it commits: a new product, a finished job, a message in an inbox.
- You map a Module's SSE endpoint and decide which Channels an Actor may listen on.
It builds on PouryaKit.Web and on the Unit of Work of PouryaKit.Persistence.EFCore, and its pushes come from the Notification Handlers of PouryaKit.Messaging.
Quick start
Register the listener in the Host, over the database the Modules store in:
builder.Services.AddPouryaKitRealtime(connectionName: "app");Map the Module's SSE endpoint and the rule that admits an Actor to a Channel:
internal sealed class NewProductsRealtime : IEndpoint
{
public void Map(IEndpointRouteBuilder routes) =>
routes.MapPouryaKitRealtime("/events", (_, channel) => channel == NewProduct.Channel);
}Push from a Notification Handler, as the Proving Sample does:
internal sealed class AnnounceNewProductNotificationHandler(
INewProductAnnouncer announcer,
IRealtimePublisher realtime
) : INotificationHandler<ProductCreatedDomainEvent>
{
public async Task<Result> Handle(
ProductCreatedDomainEvent domainEvent,
CancellationToken cancellationToken
)
{
await announcer.AnnounceAsync(
new NewProductAnnouncement(domainEvent.ProductId.Value, domainEvent.Name),
cancellationToken
);
return await realtime.PublishAsync(
NewProduct.Channel,
new NewProduct(domainEvent.ProductId.Value, domainEvent.Name),
cancellationToken
);
}
}What you get
- A push that reaches clients only once its delivery commits, so a rollback pushes nothing.
- Every instance of the Host hears it, through the database the Modules already use.
- Pushes held to the delivery's tenant, on Channels the endpoint's rule admits.
- No SignalR and no second channel to the client.
Delivery to clients is at most once. A client receives a push only while it is connected, and nothing is stored or replayed: when it connects again it reads its state through a query.
Keep each push a small fact or a reference; the client reads the rest.