Listen for events

Subscribe with bdk.on(event, listener) to receive the results native features report back.

Subscribe to an event

Examples reuse the single browser client from Set up the client. Adjust the ~/lib/bdk import to your app’s path; do not create a client in each component or handler.

Register a listener for a native event. The payload is typed from the event name (see BdkNativeEvents). Dot-named events such as iap.purchaseCompleted and deeplink.received use the same bdk.on call. Subscribe when the shared client loads so you don't miss a result. bdk.on returns an unsubscribe function — call it on unmount.

import { bdk } from "~/lib/bdk";

const off = bdk.on("deviceInfo", (info) =>
  console.log(info.deviceOS, info.bdkRelease, info.playerId)
);

off(); // stop listening

Get the result of a call

Subscribe before you call the method. Awaiting the call confirms it was dispatched, not that it succeeded — the result arrives on the event. Some events fire quickly (photos, barcode, location, pickers, popups, contacts, screenshots); others may resolve much later or never (purchases, receipts, biometrics, Smart Login).

iap.purchaseIos / purchaseAndroid take { id, type }, where type is "product" or "subscription". Match these key names exactly — they are the native contract.

bdk.on("barcodeScanned", (code) => console.log("scanned", code));
bdk.on("location", (loc) => console.log("location", loc));

await bdk.media.scanBarcode();
await bdk.location.getCurrentPosition();

All events and payloads

Every event and the payload its listener receives. The field-level shape of each object is in Objects.

EventListener receives
deviceInfoBdkDeviceInfo
photoSelected / photoCapturedMediaResult
audioRecordedMediaResult
screenshotthe captured screenshot image (a data URI you can preview or upload)
barcodeScannedthe scanned code: its type and decoded content
contactsthe device address book: each contact's name, phone numbers, and emails
locationthe device position as a "latitude,longitude" string or an object with latitude and longitude
backgroundLocationEnabled{ enabled, alreadyRunning, reason }
backgroundLocationDisabled{ enabled }
deviceVariable{ name, data }
menuClickedthe menu item the user tapped
popupClosedwhich button dismissed the popup
datePickedthe date/time the user selected
backButtonPressedundefined
biometricResult{ data, status, platform }
smartLoginCredentials{ email, password }
purchaseSuccess / purchaseFailed{ platform, data }
receiptReceived{ platform, data }
capabilitiesBdkCapabilities
permissions.changed{ changed, previous, permissions }
att.changed{ status, idfa }
deeplink.receivedBdkDeepLinkRecord
navigation.dismissedNavigationDismissedEvent
auth.completed{ mode, provider, handoff }
auth.cancelled{ reason }
push.clickedBdkPushClickRecord, without delivered, plus event and ts; see Handle notification taps.
push.dataReceived{ id, receivedAt, appState, data }
share.received{ share } (BdkInboundShare)
share.uploadProgress{ shareId, itemIndex, bytesSent, totalBytes, progress }
nfc.launchTag{ url, tag }
iap.purchaseCompleted{ platform, requestId, ok, state, code, transaction }
iap.transactionUpdated{ reason, transaction }
errorBdkError
// The payload type is inferred from the event name.
bdk.on("deviceInfo", (info) => info.playerId);          // BdkDeviceInfo
bdk.on("photoCaptured", (photo) => photo.fileUrl);       // MediaResult
bdk.on("iap.purchaseCompleted", (e) => e.state);         // { platform, requestId, ok, state, ... }
bdk.on("navigation.dismissed", (e) => e.reason);         // NavigationDismissedEvent
bdk.on("biometricResult", ({ status, platform }) => {}); // { data, status, platform }
bdk.on("error", (err) => err.code);                      // BdkError

Handle errors

Configure onError in the shared client module.

A throwing listener doesn't break the others. Catch errors centrally via the error event or the onError config callback — both receive a BdkError.

import { bdk } from "~/lib/bdk";

bdk.on("error", (err) => console.error(err.code, err.message, err.details));

// Set onError in the shared module; see Set up the client.

See every native feature with its demo on thebdk.com.