HTTP Reaction
HTTP Reaction is a plugin component for Drasi that sends HTTP webhooks when query results change. It enables real-time notifications to external HTTP endpoints whenever continuous queries detect additions, updates, or deletions in the result set.
Overview
The HTTP Reaction component monitors continuous query results and automatically sends HTTP requests to configured endpoints when data changes occur. It supports:
- Multiple HTTP Methods: GET, POST, PUT, DELETE, PATCH
- Flexible Routing: Different endpoints for different queries and operation types
- Template-Based Requests: Handlebars templates for dynamic URL, body, and header generation
- Authentication: Built-in Bearer token authentication support
- Operation-Specific Handling: Separate configurations for ADD, UPDATE, and DELETE operations
- Priority Queue Processing: Processes changes in timestamp order to ensure correct sequencing
Use Cases
- Real-time Notifications: Alert external systems when data changes
- Workflow Automation: Trigger automated workflows based on query result changes
- Data Synchronization: Keep external systems in sync with Drasi query results
- Event-Driven Architecture: Integrate Drasi with event-driven applications
- Monitoring and Alerting: Send alerts when specific conditions are detected
Configuration
Builder Pattern (Recommended)
The builder pattern provides a fluent API for programmatic configuration:
use HttpReaction;
let reaction = builder
.with_base_url
.with_token
.with_timeout_ms
.with_query
.with_query
.build?;
Config Struct Approach
For more complex configurations with custom routing:
use ;
use HashMap;
let mut routes = new;
routes.insert;
let config = HttpReactionConfig ;
let reaction = new;
Configuration Options
HttpReactionConfig
| Name | Description | Type | Default | Required |
|---|---|---|---|---|
base_url |
Base URL for all HTTP requests. Can be overridden with absolute URLs in CallSpec. | String | "http://localhost" |
No |
token |
Bearer token for authentication. Automatically adds Authorization: Bearer <token> header. |
Option<String> | None | No |
timeout_ms |
Request timeout in milliseconds. | u64 | 5000 | No |
routes |
Query-specific routing configurations. Keys are query IDs. | HashMap<String, QueryConfig> | Empty | No |
QueryConfig
Defines HTTP call specifications for each operation type within a query.
| Name | Description | Type | Required |
|---|---|---|---|
added |
HTTP call specification for ADD operations (new rows). | Option<CallSpec> | No |
updated |
HTTP call specification for UPDATE operations (modified rows). | Option<CallSpec> | No |
deleted |
HTTP call specification for DELETE operations (removed rows). | Option<CallSpec> | No |
CallSpec
Specification for an individual HTTP call.
| Name | Description | Type | Default | Required |
|---|---|---|---|---|
url |
URL path (appended to base_url) or absolute URL. Supports Handlebars templates. | String | - | Yes |
method |
HTTP method: GET, POST, PUT, DELETE, or PATCH (case-insensitive). | String | - | Yes |
body |
Request body as a Handlebars template. If empty, sends raw JSON data. | String | Empty | No |
headers |
Additional HTTP headers. Values support Handlebars templates. | HashMap<String, String> | Empty | No |
Builder Methods
| Method | Description | Parameters |
|---|---|---|
builder(id) |
Create a new builder | id: impl Into<String> |
with_base_url(url) |
Set the base URL | url: impl Into<String> |
with_token(token) |
Set authentication token | token: impl Into<String> |
with_timeout_ms(ms) |
Set request timeout | ms: u64 |
with_query(id) |
Add a query to subscribe to | id: impl Into<String> |
with_queries(ids) |
Set all queries to subscribe to | ids: Vec<String> |
with_route(id, config) |
Add a route configuration | id: impl Into<String>, config: QueryConfig |
with_priority_queue_capacity(capacity) |
Set priority queue capacity | capacity: usize |
with_auto_start(auto_start) |
Enable/disable auto-start | auto_start: bool |
build() |
Build the HttpReaction instance | Returns anyhow::Result<HttpReaction> |
Output Schema
The HTTP Reaction sends JSON payloads to configured endpoints. The exact format depends on whether a custom body template is specified.
Default Payload Format
When no custom body template is provided, the reaction sends the raw data:
ADD Operation:
UPDATE Operation:
DELETE Operation:
Template Variables
When using Handlebars templates in the body, the following variables are available:
| Variable | Description | Available Operations |
|---|---|---|
after |
The new/current state of the data | ADD, UPDATE |
before |
The previous state of the data | UPDATE, DELETE |
data |
The data field (equivalent to after for ADD/UPDATE) |
UPDATE |
query_name |
The ID of the query that triggered the change | ALL |
operation |
The operation type: "ADD", "UPDATE", or "DELETE" | ALL |
HTTP Request Details
Headers:
Content-Type: application/json(always set)Authorization: Bearer <token>(if token is configured)- Any custom headers defined in CallSpec
URL Construction:
- If CallSpec.url starts with
http://orhttps://, it's used as-is - Otherwise, it's appended to the base_url:
{base_url}{CallSpec.url}
Usage Examples
Example 1: Basic Webhook
Simple webhook that POSTs all changes to a single endpoint:
use HttpReaction;
let reaction = builder
.with_base_url
.with_query
.build?;
This uses the default configuration which sends all ADD, UPDATE, and DELETE operations to /changes/my-query.
Example 2: Authenticated API Integration
Send changes to an authenticated API with custom timeout:
use HttpReaction;
let reaction = builder
.with_base_url
.with_token
.with_timeout_ms // 30 second timeout
.with_queries
.build?;
Example 3: Custom Routes with Templates
Different endpoints for different operations with custom payloads:
use ;
use HashMap;
let mut routes = new;
routes.insert;
let config = HttpReactionConfig ;
let reaction = new;
Example 4: Using the json Helper
The json Handlebars helper serializes complex objects:
use ;
use HashMap;
let call_spec = CallSpec ;
This ensures the entire after object is properly JSON-serialized.
Example 5: Custom Headers
Add custom headers with templating:
use ;
use HashMap;
let mut headers = new;
headers.insert;
headers.insert;
headers.insert;
let call_spec = CallSpec ;
Example 6: Integration with DrasiLib
Full example showing integration with DrasiLib:
use DrasiLib;
use HttpReaction;
// Create Drasi instance
let drasi = new.await?;
// Create and add HTTP reaction
let reaction = builder
.with_base_url
.with_token
.with_query
.build?;
drasi.add_reaction.await?;
// Start the reaction
drasi.start_reaction.await?;
Advanced Features
Priority Queue Processing
The HTTP Reaction uses a priority queue to process changes in timestamp order. This ensures that changes are sent to webhooks in the correct sequence, even if they arrive out of order.
You can customize the priority queue capacity:
let reaction = builder
.with_base_url
.with_priority_queue_capacity // Custom capacity
.build?;
Default Routing Behavior
If no route configuration is provided for a query, the HTTP Reaction uses a default configuration that:
- POSTs all ADD operations to
/changes/{query_id} - POSTs all UPDATE operations to
/changes/{query_id} - POSTs all DELETE operations to
/changes/{query_id}
This allows you to quickly set up basic webhooks without detailed configuration.
Query ID Matching
The HTTP Reaction supports flexible query ID matching:
- Exact match:
"my-query"matches query ID"my-query" - Suffix match: If query ID is
"source.my-query", it will match routes for"my-query"
This is useful when queries are namespaced by source.
Error Handling
The HTTP Reaction logs errors but continues processing:
- Failed HTTP requests are logged as warnings with status code and response body
- Processing errors are logged as errors but don't stop the reaction
- The reaction continues processing subsequent changes even if individual requests fail
Performance Considerations
- Timeout Configuration: Set appropriate timeouts based on your endpoint's expected response time
- Priority Queue Capacity: Adjust based on expected change volume and processing speed
- Concurrent Processing: The reaction processes changes sequentially within each query to maintain order
- HTTP Client: Uses a shared reqwest client with connection pooling for efficiency
Dependencies
The HTTP Reaction requires the following dependencies:
drasi-lib- Core Drasi library for plugin integrationreqwest- HTTP client libraryhandlebars- Template engine for dynamic contentserde/serde_json- JSON serializationtokio- Async runtimeanyhow- Error handling
Plugin Packaging
This reaction is compiled as a dynamic plugin (cdylib) that can be loaded by drasi-server at runtime.
Key files:
Cargo.toml— includescrate-type = ["lib", "cdylib"]src/descriptor.rs— implementsReactionPluginDescriptorwith kind"http", configuration DTO, and OpenAPI schema generationsrc/lib.rs— invokesdrasi_plugin_sdk::export_plugin!to export the plugin entry point
Building:
The compiled .so (Linux) / .dylib (macOS) / .dll (Windows) is placed in target/debug/ and can be copied to the server's plugins/ directory.
For more details on the plugin descriptor pattern and configuration DTOs, see the Reaction Developer Guide.
License
Copyright 2025 The Drasi Authors.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.