DariInterceptor API
Interface and default implementation for capturing bridge messages
This page covers the manual way, the default. To capture without Dari calls in bridge code, keep the same dependencies and add the Gradle plugin. It makes these same calls for you.
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. Call it once where every request enters the bridge, before dispatching to a handler.
| Parameter | Type | Description |
|---|---|---|
handlerName | String | Bridge handler name |
requestId | String? | Unique ID for pairing with a response. null = no response is matched; the entry stays IN_PROGRESS unless fireAndForget resolves it |
requestData | String? | Request payload |
fireAndForget | Boolean? | null = use global config, true = resolve immediately, false = wait for response |
onWebToAppResponse
Called when the native side sends a response back to JavaScript. Call it in the one function that sends every response, so handlers need no call of their own.
| Parameter | Type | Description |
|---|---|---|
handlerName | String | Must match the request |
requestId | String? | Must match the request's requestId |
responseData | String? | Response payload |
isSuccess | Boolean | true → SUCCESS, false → ERROR |
Dari records the response when this is called, not when the page receives it. Whether the page received it is tracked separately as delivery; see onDeliveryChanged.
onAppToWebRequest
Called when the native side pushes a message to JavaScript. Call it in the function that sends messages to the web.
onAppToWebResponse
Called when JavaScript responds to a native-initiated message. Call it where the web's reply arrives.
onDeliveryChanged
Called with PENDING, DELIVERED or FAILED once it's known whether a Web → App response or an App → Web request reached the page. direction says which. The detail views show it as Delivery: a message still pending 5 seconds after it was sent reads as not confirmed, for example a response the app dropped because its WebView was gone.
The Gradle plugin and the iOS SDK's automatic capture call it for you. With a manual integration, call it from your evaluateJavascript callback:
interceptor?.onDeliveryChanged(requestId, MessageDirection.WEB_TO_APP, DeliveryStatus.PENDING)
webView.evaluateJavascript(script) {
interceptor?.onDeliveryChanged(requestId, MessageDirection.WEB_TO_APP, DeliveryStatus.DELIVERED)
}On Android, the callback can't tell a script that threw from one that returned nothing, so FAILED only comes from iOS.
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.
Page URL and origin
Report the page so each message records which web deployment sent it. Add doUpdateVisitedHistory, which also fires on SPA history changes, to the WebViewClient your WebView already uses; assigning a new client would drop its navigation and auth callbacks:
override fun doUpdateVisitedHistory(view: WebView, url: String?, isReload: Boolean) {
super.doUpdateVisitedHistory(view, url, isReload)
interceptor?.onPageUrlChanged(url)
}Requests captured afterwards store the URL without user info, query or fragment, which can carry credentials or tokens. Both inspectors show it in the detail view, add an origin filter once more than one origin appears, and scope Response Time stats to the picked origin. Exports include it as page_url. Use one interceptor per WebView so pages don't overwrite each other.
androidx.web WebContent
With androidx.web's WebContent, a page outlives its views: every attach() returns a new WebContentView for the same engine. Create the interceptor once next to the retained WebContent (for example in the same ViewModel), not once per view. Report the URL from the engine's WebViewClient, which survives re-attach.
The Gradle plugin keys the page by the engine's WebViewClient, which survives re-attach. A new WebContentView takes over the page's interceptors and URL when it reports a URL or gets a JavaScript interface. If the app sets a new client on every attach, the page goes to the new view only when no attached view shows it and the view's history continues the page's history. Give each WebContent its own WebViewClient instance: engines sharing one, such as a singleton, are recorded as one page. Until the new view reports, calls go to the previous view; if that view was already collected, they stay on the page the bridge was registered on. The WebView seen last is used only when neither is known, and calls are recorded without a page URL only when there is none.
A response produced while no view is attached is still recorded as SUCCESS, even if the app drops it because it has no view to call evaluateJavascript on. Dari records the call, not its delivery.
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)