Dari

DariInterceptor API

Interface and default implementation for capturing bridge messages

Interface

interface DariInterceptor {
    val tag: String? get() = null

    fun onWebToAppRequest(
        handlerName: String,
        requestId: String?,
        requestData: String?,
        fireAndForget: Boolean? = null,
    )

    fun onWebToAppRequest(
        handlerName: String,
        displayName: String?,
        requestId: String?,
        requestData: String?,
        fireAndForget: Boolean? = null,
    )

    fun onWebToAppResponse(
        handlerName: String,
        requestId: String?,
        responseData: String?,
        isSuccess: Boolean,
    )

    fun onAppToWebRequest(
        handlerName: String,
        requestId: String?,
        data: String?,
        fireAndForget: Boolean? = null,
    )

    fun onAppToWebRequest(
        handlerName: String,
        displayName: String?,
        requestId: String?,
        data: String?,
        fireAndForget: Boolean? = null,
    )

    fun onAppToWebResponse(
        requestId: String?,
        isSuccess: Boolean,
        responseData: String?,
    )
}

DefaultDariInterceptor

DefaultDariInterceptor is the debug implementation. It stores messages in Dari's Room database and posts notifications.

val interceptor = Dari.createInterceptor(tag = "PaymentBridge")

Use Dari.createInterceptor() in app code so release builds compile against dari-noop. The optional tag labels messages by bridge source when multiple interceptors are active in the same app.

Protocol Buffers (optional)

Protobuf inspection has no feature flag and does not replace the string/JSON API. See the Protocol Buffers guide for activation requirements, decoder setup, bridge transport options, and all request/response methods.

Methods

onWebToAppRequest

Called when JavaScript sends a message to the native side.

ParameterTypeDescription
handlerNameStringBridge handler name
requestIdString?Unique ID for pairing with a response. null = standalone
requestDataString?Request payload
fireAndForgetBoolean?null = use global config, true = resolve immediately, false = wait for response

onWebToAppResponse

Called when the native side sends a response back to JavaScript.

ParameterTypeDescription
handlerNameStringMust match the request
requestIdString?Must match the request's requestId
responseDataString?Response payload
isSuccessBooleantrue → SUCCESS, false → ERROR

onAppToWebRequest

Called when the native side pushes a message to JavaScript.

onAppToWebResponse

Called when JavaScript responds to a native-initiated message.

Optional display names

Use the request overload with displayName when a stable handler identifier needs a human-readable label:

interceptor?.onWebToAppRequest(
    handlerName = "N:4-1",
    displayName = "Today's health goal",
    requestId = requestId,
    requestData = payload,
)

Dari stores and exports both values separately, searches both, and renders N:4-1(Today's health goal) in the message list and notification. Blank display names behave like null. The same overload is available for App-to-Web and protobuf requests; existing signatures remain supported.

Fire-and-Forget

One-way calls that never receive a response can be marked so they immediately resolve to SUCCESS:

// Per-call
interceptor?.onWebToAppRequest(
    handlerName = "logEvent",
    requestId = null,
    requestData = """{"event": "button_click"}""",
    fireAndForget = true,
)

// Global setting: all calls
DariConfig(fireAndForget = true)

On this page