Skip to content

Commit 9741cba

Browse files
committed
Introduce EventPublisher trait.
The LDK Server needs a flexible and reliable mechanism to publish informational events (e.g., transaction updates, channel status changes) to external messaging systems to integration with other external services in a decoupled manner. To achieve this, we introduce the EventPublisher trait, which defines a consistent, asynchronous interface for event publishing. This allows the daemon to support multiple messaging backends (e.g., RabbitMQ, Kafka, AWS SQS) via feature flags. Since underlying messaging systems are expected to support durable buffering, this keeps the LDK Server decoupled from event storage and handling, while enabling multiple consumers to process events independently. This is in contrast with in-memory event queues, polling, or WebSockets, since they either lack durability, tightly couple the daemon to consumers, or require constant connectivity. In-memory queues risk losing events on restart, polling burdens the daemon with service calls from each consumer, and WebSockets demand real-time client availability—none of which support multiple consumers efficiently.
1 parent 3fe2b1d commit 9741cba

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ldk-server/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ bytes = { version = "1.4.0", default-features = false }
1717
hex = { package = "hex-conservative", version = "0.2.1", default-features = false }
1818
rusqlite = { version = "0.31.0", features = ["bundled"] }
1919
rand = { version = "0.8.5", default-features = false }
20+
async-trait = { version = "0.1.85", default-features = false }
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use crate::api::error::LdkServerError;
2+
use async_trait::async_trait;
3+
use ldk_server_protos::events::EventEnvelope;
4+
5+
/// A trait for publishing events or notifications from the LDK Server.
6+
///
7+
/// Implementors of this trait define how events are sent to various messaging
8+
/// systems. It provides a consistent, asynchronous interface for event publishing, while allowing
9+
/// each implementation to manage its own initialization and configuration, typically sourced from
10+
/// the `ldk-server.config` file. A no-op implementation is included by default,
11+
/// with specific implementations enabled via feature flags.
12+
///
13+
/// Events are represented as [`EventEnvelope`] messages, which are Protocol Buffers
14+
/// ([protobuf](https://protobuf.dev/)) objects defined in [`ldk_server_protos::events`].
15+
/// These events are serialized to bytes by the publisher before transmission, and consumers can
16+
/// deserialize them using the protobuf definitions.
17+
///
18+
/// The underlying messaging system is expected to support durably buffered events,
19+
/// enabling easy decoupling between the LDK Server and event consumers.
20+
#[async_trait]
21+
pub trait EventPublisher {
22+
/// Publishes an event to the underlying messaging system.
23+
///
24+
/// # Arguments
25+
/// * `event` - The event message to publish, provided as an [`EventEnvelope`]
26+
/// defined in [`ldk_server_protos::events`]. Implementors must serialize
27+
/// the whole [`EventEnvelope`] to bytes before publishing.
28+
///
29+
/// In order to ensure no events are lost, implementors of this trait must publish events
30+
/// durably to underlying messaging system. An event is considered published when
31+
/// [`EventPublisher::publish`] returns `Ok(())`, thus implementors MUST durably persist/publish events *before*
32+
/// returning `Ok(())`.
33+
///
34+
/// # Errors
35+
/// May return an [`LdkServerErrorCode::InternalServerError`] if the event cannot be published,
36+
/// such as due to network failures, misconfiguration, or transport-specific issues.
37+
/// If event publishing fails, the LDK Server will retry publishing the event indefinitely, which
38+
/// may degrade performance until the underlying messaging system is operational again.
39+
///
40+
/// [`LdkServerErrorCode::InternalServerError`]: crate::api::error::LdkServerErrorCode
41+
async fn publish(&self, event: EventEnvelope) -> Result<(), LdkServerError>;
42+
}

ldk-server/src/io/events/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub(crate) mod event_publisher;

ldk-server/src/io/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
pub(crate) mod events;
12
pub(crate) mod persist;
23
pub(crate) mod utils;

0 commit comments

Comments
 (0)