Portal Community

What Are Interaction Hooks?

EdgeInteract hooks extend the IInteractionHook interface and are registered in the DI container. The pipeline invokes all registered hooks at the appropriate lifecycle points. Hooks can inspect, modify, or block interactions without being coupled to application code.

Hook Interface

public interface IInteractionHook
{
    /// Runs before the InteractionRequest is delivered via EdgeStream.
    /// Throw InteractionBlockedException to prevent delivery.
    Task OnBeforePublish(InteractionRequest request, CancellationToken ct);

    /// Runs after an InteractionResponse is received and validated.
    /// Cannot block the cycle, but can perform side effects.
    Task OnAfterRespond(
        InteractionRequest request,
        InteractionResponse response,
        CancellationToken ct);
}

Built-in Hooks

HookTypePurposeRequired?
AuditInteractionHookBothLogs every request/response pair to the audit storeRecommended
RateLimitInteractionHookPre-sendPrevents flooding a user with too many interactionsRecommended
ResponseValidationHookPost-receiveValidates response outcome and data schema per typeAlways enabled
MetricsInteractionHookBothEmits OTel metrics for each lifecycle eventOptional

Hook Lifecycle Points

1

OnBeforePublish

Runs after request validation but before EdgeStream delivery. All registered hooks run in sequence. Any hook can throw InteractionBlockedException to abort publication.

2

[Interaction Lifecycle — Delivery → Display → Respond]

No hooks run during the client-side stages. The pipeline is silent until the response arrives.

3

OnAfterRespond

Runs after the response arrives and the response validation hook approves it. Cannot block. All hooks run in sequence. Exceptions are logged but do not fail the interaction cycle.

Registering Hooks

// Program.cs
builder.Services.AddEdgeInteract(options => {
    options.DefaultTimeoutMs = 86_400_000;
});

// Register built-in hooks
builder.Services.AddInteractionHook<AuditInteractionHook>();
builder.Services.AddInteractionHook<RateLimitInteractionHook>();
builder.Services.AddInteractionHook<MetricsInteractionHook>();

// Register custom hooks
builder.Services.AddInteractionHook<MyEnrichmentHook>();
builder.Services.AddInteractionHook<MyComplianceHook>();
Hook Order Matters Hooks run in the order they are registered. Rate limiting should run before audit logging (so blocked interactions are not logged as valid publishes). See the Hook Execution Order page for the recommended sequence.