SubscriptionOnCreate is an experimental hook. Its signature and behaviour may change without prior notice.
The SubscriptionOnCreate handler is a custom module hook that runs once per subscription, before the subscription is registered with the message broker. It gives you access to the subscription event configuration and lets you modify it in place.
This handler is useful for:
- Dynamic subject routing: Override NATS subjects, Redis channels or Kafka topics based on custom logic
- Per-request configuration: Override provider-level defaults for individual subscriptions
Limitations
Compared to SubscriptionOnStart, this handler:
- Cannot emit events to the client
- Only applies to Cosmo Streams / EDFS subscriptions, not to regular GraphQL subscriptions
Handler Interface
Implement SubscriptionOnCreateHandler in a Custom Module to use this hook.
Modifying the Configuration
SubscriptionEventConfiguration is an interface. To access provider-specific fields you must type-assert to the concrete configuration type for your message broker.
For NATS subscriptions:
For Redis subscriptions:
Use ctx.SubscriptionEventConfiguration().ProviderType() to branch by provider type when your module handles multiple providers.
Usage Example
Dynamic NATS subject routing
The following example rewrites NATS subjects based on a tenant identifier from the request header. Each tenant is isolated to its own subject namespace.
Dynamic Redis channel routing
The following example rewrites Redis channels based on a user ID extracted from the authenticated request. Each user receives events only from their own channel.