> ## Documentation Index
> Fetch the complete documentation index at: https://docs.platform.gamezop.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Client-side callback setup

> Receive Gamezop game events in your app or website on Web, Android, iOS, React Native, and Flutter.

Client-side callbacks let your app or website react to what happens inside a Gamezop game, such as a score change or a match result. This guide shows you how to receive those events on each platform.

The setup is the same for Individual Games and Battles. Once it's done, use the callback reference for your offering to see each event and what it contains.

> [card] [Gamezop PostMessage examples](https://github.com/gamezop/gamezop-postmessage-examples)
>
> Complete, runnable examples for Web, Android, iOS, React Native, and Flutter, with a built-in event inspector.

## Before you start

> [!NOTE]
>
> Client-side callbacks must be enabled for your Property ID before you load the game. Contact your Gamezop Account Manager to enable them. You only need to do this once, and it covers both Individual Games and Battles.

You also need the game URL that your app or website will load. Generate it by following the [Individual Games integration](https://docs.platform.gamezop.com/publishers/gamezop/offerings/individual-games/integration.md) or the [Battles integration](https://docs.platform.gamezop.com/publishers/gamezop/offerings/battles/integration/overview.md).

***

## Set up your callback handler

The callback handler is the small piece of code in your app or website that receives events from the game. Register it **before** you load the game URL, so that no events are missed.

In the snippets below, `capture` stands for your own function that handles each event: it parses the payload and passes it on to your UI or analytics code. See [Parse and route callbacks](#parse-and-route-callbacks) for how to parse payloads safely. Each platform tab links to a complete example with the full app, parsing, and lifecycle code.

> [!NOTE]
>
> Pick the tab for whatever loads the game directly:
>
> * **Your app's WebView loads the game URL:** use the tab for your platform (Android, iOS, React Native, or Flutter).
> * **Your app opens your own website, and that website loads the game in an iframe:** use the **Web** tab, even inside a mobile app.

> [tab] Web
>
> The game runs in an iframe on your page and sends each event with `window.parent.postMessage(payload, targetOrigin)`. Your page receives it in `event.data`, as either an object or a JSON string.
>
> Ask your Gamezop Account Manager to whitelist the domain that embeds the game, including any staging domains. This is a separate setting from enabling client-side callbacks, so make sure both are done. Host your page on HTTPS. A page on `http://localhost` is fine for checking the example's layout, but may not receive live events.
>
> Add the iframe to your page, then register the listener before you set the iframe's URL:
>
> ```html HTML theme={null}
> <iframe id="game-frame" title="Gamezop game"
>   referrerpolicy="strict-origin-when-cross-origin"
>   allowfullscreen></iframe>
> ```
>
> ```javascript JavaScript theme={null}
> // gameUrl is the final HTTPS game URL supplied by your integration.
> const frame = document.getElementById('game-frame');
> const expectedOrigin = new URL(gameUrl).origin;
>
> function onGameMessage(event) {
>   if (event.source !== frame.contentWindow) return;
>   if (event.origin !== expectedOrigin) return;
>   capture(event.data);
> }
>
> window.addEventListener('message', onGameMessage);
> frame.src = gameUrl;
>
> // When your host view is disposed:
> window.removeEventListener('message', onGameMessage);
> ```
>
> Keep both checks in `onGameMessage`. The source check makes sure the message came from your game iframe, and the origin check makes sure it came from the Gamezop game's domain. Gamezop only sends events to your page's domain, which is why that domain must be whitelisted. If the game URL redirects to a different domain, ask your Gamezop Account Manager for the final game URL and use its domain as `expectedOrigin`.
>
> [Open the complete Web example and setup instructions](https://github.com/gamezop/gamezop-postmessage-examples/tree/main/web).

> [tab] Android
>
> Add a JavaScript interface named `AndroidBridge`, with a `postMessage` method, to your WebView. Gamezop calls it with each event as a JSON string. WebView calls the bridge on a background thread, so the example below passes each event to the main thread before it reaches your UI.
>
> ```kotlin Kotlin theme={null}
> import android.os.Handler
> import android.os.Looper
> import android.webkit.JavascriptInterface
> import androidx.annotation.Keep
>
> @Keep
> class GameEventBridge(private val onEvent: (String) -> Unit) {
>     private val mainHandler = Handler(Looper.getMainLooper())
>
>     @Keep
>     @JavascriptInterface
>     fun postMessage(payload: String) {
>         mainHandler.post { onEvent(payload) }
>     }
> }
> ```
>
> Register the bridge on your WebView before you load `gameUrl`:
>
> ```kotlin Kotlin theme={null}
> webView.settings.javaScriptEnabled = true
> webView.settings.domStorageEnabled = true
> webView.addJavascriptInterface(GameEventBridge(::capture), "AndroidBridge")
> webView.loadUrl(gameUrl)
> ```
>
> If you shrink release builds with R8 or ProGuard, make sure the bridge's `postMessage` method is kept, as the `@Keep` annotations above do. When the game screen closes, remove `AndroidBridge`, stop loading, and destroy the WebView. The complete example also shows how to restrict navigation and which keep rules to use for release builds.
>
> [Open the complete Android example and setup instructions](https://github.com/gamezop/gamezop-postmessage-examples/tree/main/android).

> [tab] iOS
>
> Register a `WKScriptMessageHandler` named `observer`. Gamezop sends each event with `window.webkit.messageHandlers.observer.postMessage(payload)`, and `message.body` contains either an object or a JSON string.
>
> ```swift Swift theme={null}
> import WebKit
>
> final class GameEventReceiver: NSObject, WKScriptMessageHandler {
>     private let capture: (Any) -> Void
>
>     init(capture: @escaping (Any) -> Void) {
>         self.capture = capture
>         super.init()
>     }
>
>     func userContentController(_ userContentController: WKUserContentController,
>                                didReceive message: WKScriptMessage) {
>         guard message.name == "observer" else { return }
>         capture(message.body)
>     }
> }
> ```
>
> Add the message handler to the configuration before you create the WebView and load the game:
>
> ```swift Swift theme={null}
> // gameURL is your validated HTTPS URL; capture is your event handler.
> let configuration = WKWebViewConfiguration()
> let receiver = GameEventReceiver(capture: capture)
> configuration.userContentController.add(receiver, name: "observer")
> let webView = WKWebView(frame: .zero, configuration: configuration)
> webView.load(URLRequest(url: gameURL))
> ```
>
> Then add the WebView to your app's layout. When the game screen closes, remove the `observer` message handler and clear the WebView's delegates, as shown in the complete example.
>
> [Open the complete iOS example and setup instructions](https://github.com/gamezop/gamezop-postmessage-examples/tree/main/ios).

> [tab] React Native
>
> Use `react-native-webview`, which works on both Android and iOS. Setting the `onMessage` prop enables `ReactNativeWebView.postMessage` inside the WebView, and each event arrives as a JSON string in `event.nativeEvent.data`.
>
> ```tsx TypeScript theme={null}
> import { WebView } from 'react-native-webview';
>
> // Inside your component; gameUrl and capture come from your app.
> return (
>   <WebView
>     source={{ uri: gameUrl }}
>     javaScriptEnabled
>     onMessage={event => capture(event.nativeEvent.data)}
>   />
> );
> ```
>
> Set `onMessage` from the first render, not later. Keep the WebView mounted while you handle events, so that UI updates don't restart the game. The same code works on Android and iOS.
>
> [Open the complete React Native example and setup instructions](https://github.com/gamezop/gamezop-postmessage-examples/tree/main/react-native).

> [tab] Flutter
>
> Use `webview_flutter`, which works on both Android and iOS. Add a JavaScript channel named `FlutterWebView`. Gamezop sends each event with `FlutterWebView.postMessage`, and your Dart callback receives it as a JSON string in `message.message`.
>
> ```dart Dart theme={null}
> import 'package:webview_flutter/webview_flutter.dart';
>
> // Create once in your widget's initialization, before loading the game.
> final controller = WebViewController()
>   ..setJavaScriptMode(JavaScriptMode.unrestricted)
>   ..addJavaScriptChannel(
>     'FlutterWebView',
>     onMessageReceived: (message) => capture(message.message),
>   )
>   ..loadRequest(Uri.parse(gameUrl));
>
> // Return this from your widget's build method:
> // WebViewWidget(controller: controller)
> ```
>
> Create the controller once and keep it for as long as the game screen is open. Don't recreate it on every event or widget rebuild. The same channel works on Android and iOS. See the complete example for how to manage the host widget's lifecycle.
>
> [Open the complete Flutter example and setup instructions](https://github.com/gamezop/gamezop-postmessage-examples/tree/main/flutter).

***

## Parse and route callbacks

Depending on the platform, each event arrives as either a JSON string or an object. In your `capture` function:

* If the payload is a JSON string, parse it once. If it's already an object, use it as is.
* Catch errors from malformed JSON, and check that the result is an object before you read any fields.
* Ignore fields and event names you don't recognise, so that your handler keeps working when Gamezop adds new events.

Then use the right field to tell events apart:

* **Individual Games:** Read the `state` field. See [Client-side callbacks for Individual Games](https://docs.platform.gamezop.com/publishers/gamezop/offerings/individual-games/client-side-callbacks.md) for every state and its payload.
* **Battles:** Read the `event` field. See [Client-side callbacks for Battles](https://docs.platform.gamezop.com/publishers/gamezop/offerings/battles/integration/client-side-callbacks.md) for every match event and its payload.

What you do with each event is up to you, for example updating your UI or recording analytics. When you need a trusted final result, such as before giving out rewards, use [server-side webhooks](https://docs.platform.gamezop.com/publishers/gamezop/offerings/individual-games/server-side-webhooks.md) for Individual Games, or the [match outcome APIs](https://docs.platform.gamezop.com/publishers/gamezop/offerings/battles/integration/match-outcomes.md) for Battles, instead of client-side callbacks.

***

## Verify your integration

1. Run the GitHub example for your platform, and open a game using a Property ID that has client-side callbacks enabled.
2. Play the game until it ends, making sure your score changes along the way. Open the example's **Logs** view to see each event and its raw JSON.
3. For Battles, open the game on more than one device or browser, using the same `roomId` and a different `user.puid` for each. Play through a match, and check that the events you receive match [Client-side callbacks for Battles](https://docs.platform.gamezop.com/publishers/gamezop/offerings/battles/integration/client-side-callbacks.md).

***

## Troubleshooting

* **The game loads but no events arrive:** Ask your Gamezop Account Manager to confirm that client-side callbacks, and the events you need, are enabled for your Property ID. Then check that your handler is registered before the game loads, and that its name matches exactly, including letter case: `AndroidBridge` on Android, `observer` on iOS, or `FlutterWebView` on Flutter.
* **Battles events are missing:** Check your final game URL and its `roomDetails`, and make sure the same platform callback handler is registered. Battles uses the same callback setting as Individual Games.
* **The game won't load in your web page:** Ask your Gamezop Account Manager to whitelist your hosting domain. If the browser console shows errors about CSP or `X-Frame-Options`, the game is blocked from being embedded on that domain, and changing the iframe's attributes won't fix it.
* **The iframe loads but no events arrive on Web:** Make sure your page uses HTTPS, the iframe keeps `referrerpolicy="strict-origin-when-cross-origin"` so the game can see your page's domain, and `expectedOrigin` matches the game's final domain after any redirects. Keep the source and origin checks in place.
* **Parsing fails:** Look at the raw message in the example's **Logs** view, and check whether your platform sends a string or an object before you parse it.

***

**Next steps**

> [card] [Individual Games callbacks](https://docs.platform.gamezop.com/publishers/gamezop/offerings/individual-games/client-side-callbacks.md)
>
> Read the `state` field for gameplay events such as `loaded`, `start`, `playing`, `over`, and `levelup`.

> [card] [Battles callbacks](https://docs.platform.gamezop.com/publishers/gamezop/offerings/battles/integration/client-side-callbacks.md)
>
> Read the `event` field for matchmaking, match progress, results, and navigation events.

***


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.