Embed a public Retool app into the content editor for a web page, and then pass data from the parent window into the Retool app.
Details:
I read this how-to doc: Embed web apps | Retool Docs (do we need to use Retool's React or JavaScript SDKs in order to do this?) and this community post on passing data to embedded apps. I'm having trouble with the ParentWindow query and can't get it to return anything. Is embedding apps using Retool's React or JavaScript SDKs required in order to pass data from the parent window into an embedded Retool app? What am I missing here?
Steps:
To test it out, I did the following:
Add a ParentWindow query with Selector #myCustomData to the Retool app I'm going to embed. Include a console log of the data in the transformer for this query to see what it's returning.
Create a simple HTML doc containing my <iframe> Retool app embed code and a <div id="myCustomData"> with JSON formatted content to pass into my Retool app.
Open the HTML file in Chrome and open the Chrome Devtools console.
Observe that the parent window query data is undefined
From the console, run document.querySelector('#myCustomData').textContent
Observe that there is content returned
After this, I also tried a couple more things that didn't work:
hooking up the button in my Retool app to trigger the ParentWindow query (to see if there was any different in triggering it on-click from my HTML page)
removing the transformer from the ParentWindow query (in case transformers are not supported on ParentWindow queries)
I was able to get data from ParentWindow query by using the Retool Embed SDK (tested out via CodePen, this is so cool!!)
@Darren you mentioned in office hours today that the Retool Embed SDK and React Retool packages use the built-in window.postMessage() API to do the communication between parent window and retool. In the CodePen console I see Received message from parent logs, which I never saw from my HTML page. It seems like that's the missing link and why my simple HTML doc solution won't work. Did you get a chance to check out your old apps to see whether you were/still are able to use ParentWindow queries on a plain HTML page? Thank you!
I looked back at some previous projects and it is necessary to define event handlers on the parent page, whether via custom JS or one of the packages we provide. Hopefully that isn't too large a lift for your particular use case, as the functionality is pretty slick!
I'm happy to help with any additional configuration, so don't hesitate to follow up here.
Thank you, @Darren! Could you please share your custom JS for the event handlers you defined on the parent page? Seeing an example would be really helpful. Thank you!