Gamezop PostMessage examples
Complete, runnable examples for Web, Android, iOS, React Native, and Flutter, with a built-in event inspector.
Before you start
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.
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 for how to parse payloads safely. Each platform tab links to a complete example with the full app, parsing, and lifecycle code.
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.
- Web
- Android
- iOS
- React Native
- Flutter
The game runs in an iframe on your page and sends each event with Keep both checks in
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: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.Parse and route callbacks
Depending on the platform, each event arrives as either a JSON string or an object. In yourcapture 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.
- Individual Games: Read the
statefield. See Client-side callbacks for Individual Games for every state and its payload. - Battles: Read the
eventfield. See Client-side callbacks for Battles for every match event and its payload.
Verify your integration
- Run the GitHub example for your platform, and open a game using a Property ID that has client-side callbacks enabled.
- 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.
- For Battles, open the game on more than one device or browser, using the same
roomIdand a differentuser.puidfor each. Play through a match, and check that the events you receive match Client-side callbacks for Battles.
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:
AndroidBridgeon Android,observeron iOS, orFlutterWebViewon 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, andexpectedOriginmatches 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
Individual Games callbacks
Read the
state field for gameplay events such as loaded, start, playing, over, and levelup.Battles callbacks
Read the
event field for matchmaking, match progress, results, and navigation events.