Skip to main content
This guide explains how to send events to the web journey, handle incoming requests, and build request-response flows.

Message Types Overview

GBGBridge uses three message types: All messages share the same envelope structure (BridgeMessage) with a correlationId for pairing requests and responses.

Sending Events to the Web Journey

Events are fire-and-forget messages from the native host to the web journey. Use them to notify the web journey of state changes, user actions, or lifecycle transitions.

Common Event Patterns

Handling Incoming Requests

When the web journey sends a request, GBGBridge routes it to a registered handler or adds it to pendingRequests. For capture operations, use the typed slots on BridgeHost. Setting a handler declares support and routes requests automatically:
Complete the request from your camera UI callback:
The SDK encodes CaptureResult into the bridge protocol format automatically. See the Capability Handling Guide for the full SwiftUI integration pattern.

Using Custom Capabilities or BridgeCapabilityHandler

For non-camera capabilities, use registerCustomCapability() or implement BridgeCapabilityHandler:

Handling Requests via the Delegate

For requests that don’t map to a single handler β€” or when you want centralized request handling β€” use BridgeHostDelegate.
Set the delegate on your host:

Responding to Pending Requests Manually

If a request arrives with no registered handler, it is stored in pendingRequests. You can respond to it later.

Response Patterns

When responding to a request, you supply a status, optional data, and an optional error payload. The patterns below cover the three response shapes you’ll use most: success-with-data, error, and cancellation.

Success with Data

Error with Details

User Cancellation

Unsupported Action

Acknowledged (Async Processing)

Use .acknowledged when the operation will take time and you want to inform the web journey that the request was received.

Observing All Messages

Use BridgeHostDelegate.bridgeHost(_:didReceive:) to observe every message, or subscribe to the receivedMessages publisher via Combine.

Error Handling

Bridge messaging can fail in three distinct places: when GBGBridge can’t decode an incoming message, when no WebView is attached to receive an outgoing one, and when JavaScript evaluation in the WebView itself errors out. Each surfaces through lastError so you can react in your UI.

Encoding/Decoding Errors

If GBGBridge cannot decode an incoming message, lastError is set with a description. The message is not added to receivedMessages.

WebView Not Attached

If you call send(event:data:) or respond(to:...) before attaching a WebView, lastError is set to "WebView not attached".

JavaScript Evaluation Errors

If the JavaScript evaluation fails (e.g., the web page has navigated away or the receive() function is not defined), lastError is set with the WebKit error description. Monitor errors reactively:

Next Steps