Listening to events
The SDK emits several events to provide the application with an up-to-date state of the wallet or ongoing payments.
Rust
struct CliEventListener {}
impl EventListener for CliEventListener {
fn on_event(&self, e: SdkEvent) {
info!("Received event: {:?}", e);
}
}
async fn add_event_listener(
sdk: Arc<LiquidSdk>,
listener: Box<CliEventListener>,
) -> Result<String> {
let listener_id = sdk
.add_event_listener(listener)
.await?;
Ok(listener_id)
}
Swift
class SDKEventListener: EventListener {
func onEvent(e: SdkEvent) {
print("Received event: ", e)
}
}
func addEventListener(sdk: BindingLiquidSdk, listener: SDKEventListener) throws -> String? {
let listenerId = try? sdk.addEventListener(listener: listener)
return listenerId
}
Kotlin
class SDKListener : EventListener {
override fun onEvent(e: SdkEvent) {
// Log.v("SDKListener", "Received event $e")
}
}
fun addEventListener(sdk: BindingLiquidSdk, listener: SDKListener): String? {
try {
val listenerId = sdk.addEventListener(listener)
return listenerId
} catch (e: Exception) {
// handle error
return null
}
}
React Native
const onEvent = (e: SdkEvent) => {
console.log(`Received event: ${e.type}`)
}
const listenerId = await addEventListener(onEvent)
Dart
StreamSubscription<SdkEvent>? _breezEventSubscription;
Stream<SdkEvent>? _breezEventStream;
// Initializes SDK event stream.
//
// Call once on your Dart entrypoint file, e.g.; `lib/main.dart`.
void initializeEventsStream(BindingLiquidSdk sdk) {
_breezEventStream ??= sdk.addEventListener().asBroadcastStream();
}
final _eventStreamController = StreamController<SdkEvent>.broadcast();
Stream<SdkEvent> get eventStream => _eventStreamController.stream;
// Subscribe to the event stream
void subscribeToEventStream() {
_breezEventSubscription = _breezEventStream?.listen((sdkEvent) {
_eventStreamController.add(sdkEvent);
}, onError: (e) {
_eventStreamController.addError(e);
});
}
Python
class SdkListener(EventListener):
def on_event(sdk_event: SdkEvent):
logging.debug("Received event ", sdk_event)
def add_event_listener(sdk: BindingLiquidSdk, listener: SdkListener):
try:
listener_id = sdk.add_event_listener(listener)
return listener_id
except Exception as error:
logging.error(error)
raise
Go
type SdkListener struct{}
func (SdkListener) OnEvent(e breez_sdk_liquid.SdkEvent) {
log.Printf("Received event %#v", e)
}
func AddEventListener(sdk *breez_sdk_liquid.BindingLiquidSdk, listener SdkListener) (string, error) {
return sdk.AddEventListener(listener)
}
C#
public class SdkListener : EventListener
{
public void OnEvent(SdkEvent e)
{
Console.WriteLine($"Received event {e}");
}
}
public String? AddEventListener(BindingLiquidSdk sdk, SdkListener listener)
{
try
{
var listenerId = sdk.AddEventListener(listener);
return listenerId;
}
catch (Exception)
{
// Handle error
return null;
}
}
Remove event listener
When you no longer need to listen to events, you can remove the listener.
Rust
async fn remove_event_listener(
sdk: Arc<LiquidSdk>,
listener_id: String,
) -> Result<()> {
sdk.remove_event_listener(listener_id).await?;
Ok(())
}
Swift
func removeEventListener(sdk: BindingLiquidSdk, listenerId: String) throws {
try? sdk.removeEventListener(id: listenerId)
}
Kotlin
fun removeEventListener(sdk: BindingLiquidSdk, listenerId: String) {
try {
sdk.removeEventListener(listenerId)
} catch (e: Exception) {
// handle error
}
}
React Native
await removeEventListener(listenerId)
Dart
void unsubscribeFromEventStream() {
_breezEventSubscription?.cancel();
}
Python
def remove_event_listener(sdk: BindingLiquidSdk, listener_id: str):
try:
sdk.remove_event_listener(listener_id)
except Exception as error:
logging.error(error)
raise
Go
func RemoveEventListener(sdk *breez_sdk_liquid.BindingLiquidSdk, listenerId string) error {
return sdk.RemoveEventListener(listenerId)
}
C#
public void RemoveEventListener(BindingLiquidSdk sdk, String listenerId)
{
try
{
sdk.RemoveEventListener(listenerId);
}
catch (Exception)
{
// Handle error
}
}