Skip to content

Bridge Config

Everything createBridge(config), getBridge(config), and new BridgeManager(config) accept. All keys are optional.

ts
import { createBridge } from "nbridge";

const bridge = createBridge({
  debug: true,
  defaultTimeout: 5000,
  handshake: { enabled: true },
  // ...
});

Top-level keys

KeyTypeDefaultDescription
debugbooleanfalseEnable debug logging (errors always log regardless).
defaultTimeoutnumber5000Timeout in ms for messages expecting responses.
androidInterfacestring"AndroidBridge"Name of the Android JavascriptInterface on window.
iosHandlerstring"iosBridge"Name of the WKWebView message handler under webkit.messageHandlers.
schemasSchemaRegistryundefinedMessage schema registry — enables typed, validated messaging. See Schemas.
handshakeHandshakeConfigsee belowReal-connection handshake. See Core Concepts.
middlewareMiddlewareConfig{ enabled: true }Middleware system toggle. See Middleware.
compressionCompressionConfigsee belowDeflate compression for large payloads. See Compression.
queueQueueConfigsee belowRetry queue. See Retry Queue.
batchingBatchConfigsee belowMessage batching. See Batching.
metricsMetricsConfigsee belowLive metrics collection. See Metrics.
devToolsDevToolsConfigsee belowIn-page DevTools collection. See DevTools.
webLoopbackbooleanfalseEcho transport on plain web, for local development. See Plain Web.
iframeParentOriginstringundefinedExpected parent-frame origin — restricts accepted origins and sets the postMessage target origin. Set in production iframe deployments.

Partial sub-configs

Every sub-config is merged field-by-field with the defaults below, so { enabled: true } is enough to turn a feature on with documented behaviour for everything else.

handshake

KeyTypeDefaultDescription
enabledbooleanfalseExchange handshake/ack with the host so isReady() reflects a real connection. Requires host support.
timeoutnumber10000Give up (and reject waitForReady()) after this many ms.
retryIntervalnumber500Resend the handshake every N ms until acknowledged.

middleware

KeyTypeDefaultDescription
enabledbooleantrueRun registered middleware on outgoing and incoming messages.

compression

KeyTypeDefaultDescription
enabledbooleanfalseCompress outgoing payloads. Incoming compressed payloads are always decompressed, regardless.
algorithm"gzip" | "deflate" | "br""deflate"Keep "deflate" — it is the only wire format the current implementation produces.
thresholdnumber1024Minimum JSON payload size in bytes before compression kicks in.
trackStatsbooleantrueRecord getCompressionStats() data.

queue

KeyTypeDefaultDescription
enabledbooleanfalsePark messages the adapter failed to deliver instead of failing the send.
maxSizenumber100Total queued messages across all priorities. At capacity the oldest lowest-priority entry is evicted; a message is dropped outright only when everything queued outranks it.
persistbooleanfalseSave the queue to localStorage and restore on next load (trimmed to maxSize).
storageKeystring"nbridge-queue"localStorage key used when persist is on.
autoFlushbooleantrueRetry delivery on an interval.
flushIntervalnumber5000Auto-flush interval in ms.
maxRetriesnumber3Delivery attempts before a queued message is dropped.
maxAgenumber86400000Discard entries older than this (ms), checked on load and before each flush. Infinity keeps them forever.

batching

KeyTypeDefaultDescription
enabledbooleanfalseBuffer fire-and-forget messages into __nbridge_batch__ envelopes. Requires host support.
maxSizenumber10Flush when this many messages are buffered.
maxWaitnumber100Flush this many ms after the first buffered message.

metrics

KeyTypeDefaultDescription
enabledbooleanfalseCollect live traffic metrics.
updateIntervalnumber1000Recompute and notify onMetricsUpdate listeners every N ms.
detailedTimingbooleanfalseKeep per-message timing detail.

devTools

KeyTypeDefaultDescription
enabledbooleanfalseRecord message history and logs for the DevTools panel. Automatically disabled in production builds (NODE_ENV=production).
maxMessageHistorynumber50Bridge messages kept in the history tab.
logDestination"console" | "devtools" | "both" | "none""devtools"Where bridge logs are routed.
maxConsoleLogEntriesnumber100Console entries mirrored into the panel.

Fully-loaded example

ts
import { createBridge } from "nbridge";
import { schemas } from "./schemas";

export const bridge = createBridge({
  debug: process.env.NODE_ENV !== "production",
  defaultTimeout: 5000,
  androidInterface: "AndroidBridge",
  iosHandler: "iosBridge",
  schemas,
  handshake: { enabled: true, timeout: 10_000, retryInterval: 500 },
  middleware: { enabled: true },
  compression: { enabled: true, algorithm: "deflate", threshold: 1024 },
  queue: {
    enabled: true,
    maxSize: 100,
    persist: true,
    storageKey: "nbridge-queue",
    autoFlush: true,
    flushInterval: 5000,
  },
  batching: { enabled: true, maxSize: 10, maxWait: 100 },
  metrics: { enabled: true, updateInterval: 1000, detailedTiming: false },
  devTools: {
    enabled: process.env.NODE_ENV !== "production",
    maxMessageHistory: 50,
    logDestination: "devtools",
    maxConsoleLogEntries: 100,
  },
  webLoopback: false,
  iframeParentOrigin: "https://host.example.com",
});

Released under the MIT License.