-
Notifications
You must be signed in to change notification settings - Fork 7
Home
In general, Webhooks are a common asynchronous means today for listening to key 'events' raised by online systems, typically over HTTPS.
The idea is simple:
- Register a callback URL to be called when an "event" of a certain type is going to be raised, and the system will POST you notification of that event (along with any data describing that event) to that callback URL when the event is raised.
- You first need to register the callback URL with the system for a specific event (which implies a specific DTO of the event data).
- Sit back and wait to be called at your callback URL.
Under the covers, Webhooks are a good example of pub/sub architectures, where the consumers of the events are (typically) decoupled from the producer of the events.
In this project, we are basing the subscription model of webhooks on that defined by GitHub Webhooks.
Which defines the subscription service interface, and wire representations of published events.
- Subscription Service Definition (except ping)
- Event Payloads
In this project, you will add webhooks to your ServiceStack project, by registering (and customizing) the WebhookFeature
in your AppHost.cs
public override void Configure(Container container)
{
// Register the ValidationFeature and AuthFeature first
Plugins.Add(new ValidationFeature());
Plugins.Add(new AuthFeature(...));
// Register your own IWebhookSubscriptionStore and IWebhookEventSink
container.Register<IWebhookSubscriptionStore>(new MyDbSubscriptionStore());
container.Register<IWebhookEventSink>(new MyAsyncEventSink());
Plugins.Add(new WebhookFeature
{
.. any customization for your environment
});
}
WARNING: In any production system you are going to need to register your own IWebhookSubscriptionStore
and IWebhookEventSink
, since the built-in ones are really only designed for getting up and running quickly, and for testing.
See Getting Started for more details
The WebHookFeature
installs the following things into your service:
- A singleton
IWebhooks
service that you can use in your services to raise events as they happen, using thePublish<TDto>(string eventName, TDto data)
. - A full
SubscriptionsService
service that provides all the operations for your subscribers to manage their own webhook registrations.
There are several points of extensibility in the WebhookFeature
, and each may support their own customization:
BY default the WebHookFeature
registers and configures the SubscriptionService
to provide an API for subscribers to manage their webhooks.
You can choose to turn this service off, and even ship your own, and you can also configure the authorization roles that secure it.
See WebhookFeature for more details on how to customize it.
By default, the built-in SubscriptionService
is secured like all other services in your ServiceStack AppHost (using the AuthFeature
), and it uses role-based authorization to secure the various operations.
You can, if you choose customize the roles these operations require.
See Subscription Service for more details on how to customize it.
By default, the built-in MemoryWebhookSubscriptionStore
uses an MemoryCacheClient
store for storing subscriptions.
You should register a different IWebhookSubscriptionStore
that will persist subscriptions to some permanent (and perhaps distributed) store in your architecture.
These stores are typically provided by extensions to the ServiceStack.Webhooks
framework such as those listed in: Plugins
WARNING: The MemoryWebhookSubscriptionStore
is not designed for use in production systems. If you do NOT register your own IWebhookSubscriptionStore
your subscriptions will be lost when your AppHost restarts!
By default, the built-in AppHostWebhookEventSink
will relay events to all subscribers in the same thread that called IWebhooks.Publish<TDto>()
.
You should register a different IWebhookEventSink
that will decouple the raising of events from the relays of events to subscribers, using appropriate components in your architecture. (i.e. muti-threading, queues, async hooks etc.)
These kinds of sinks are typically provided by extensions to the ServiceStack.Webhooks
framework such as those listed in: Plugins
WARNING: The AppHostWebhookEventSink
is not designed for use in production systems. If you do NOT register your own IWebhookEventSink
your services will suffer unacceptable performance penalties.