#Listening to postMessage Not Working

1 messages · Page 1 of 1 (latest)

carmine pollen
#

I have a library which sends postMessage to window.ReactNativeWebView object, like this:
Generic Example: window.ReactNativeWebView.postMessage(JSON.stringify({ command, ...(options || {}) }))

In my electron app, I want to create a ReactNativeWebView object on window object, and then listen to the messages sent by the library like this:
window.ReactNativeWebView.addEventListener();

So far I've tried so many ways and nothing seems to work. Can anyone help?

split panther
#

What exactly does not work?

carmine pollen
#

When there is a postMessage sent, the listener doesn't listen. 😦
There is no error either.

#

When I send window.ReactNativeWebView.postMessage("Some message"); from my electron app directly, it works.
But when my library sends the same window.ReactNativeWebView.postMessage("Some message"); the listeners aren't triggered.

The library is inside an iframe which I'm embedding in my electron app as webview.

split panther
#

iframe as webview?

carmine pollen
#

yes.

split panther
#

and what ReactNativeWebView is?

carmine pollen
#

Actually. Its an external webpage which embeds the iframe.

split panther
#

do you have access to iframe?

carmine pollen
#

yes ..

split panther
#

i dont think so

#

if you send postMessage you send it to webview

#

so iframe dont know anything about it

carmine pollen
#

actually I never send a postMessage. I only want to listen to the postMessage sent by the iframe.

#

iframe sends this message:
window.ReactNativeWebView.postMessage("Some message");

split panther
#

what is ReactNativeWebView?

carmine pollen
#

It does all these inside a mobile app.
The mobile app is build in ReactNative.

But I'm building a simulator electron app where people can use it to debug their app.
So I do not have ReactNativeWebView in electron. So I just want to create a ReactNativeWebView object on window object and listen to the postMessage from the iframe and simulate the behaviour.

split panther
#

webview is another browserwindow

carmine pollen
#

What I'm doing in electron is: I've embedded an iframe and named it as ReactNativeWebView. And just trying to listen to the postMessage from the iframe.

split panther
#

so you have browserwindow inside of browserwindow with iframe in it

carmine pollen
#

there is a webview inside browserwindow. The webview has an iframe in it.

split panther
#

webview is browserwindow as well

carmine pollen
#

But I'm successfully able to output the logs of the iframe inside webview.

split panther
#

now you need to resend it to parent window i suppose

carmine pollen
#

webview is browserwindow as well
No. I'm just using webview tag and putting the URL in src field.
So there is only one browserWindow and inside that I'm having webview tag.

split panther
#

webview is independend browser view

carmine pollen
#

yes.

split panther
#

just like iframe but as separeate process

carmine pollen
#

yes.

split panther
#

and it behave as independend browser window

#

or tab

carmine pollen
#

webview is independend browser view
It's an external URL. Which embeds another external URL as iframe!!

split panther
#

with independend dom tree

carmine pollen
#

yes I suppose, as they are external URLs.

split panther
#

do you have anything in webview devtools?

carmine pollen
#

My understanding was, if I create an object on window object and if any of the iframe our domain sends a postMessage, any of the windows can listen to it. No?

#

do you have anything in webview devtools?
Yes. It shows all the libraries loading status and all the console logs etc.

#

So let me summaries:
I've a browserWindow, in which there is a webview(external URL), which in turn has iframe(which is also an external URL for the webview). Now the iframe sends window.ReactNativeWebView.postMessage("Some message");

How do I listen to it in my renderer process of electron app?

split panther
#

you need to resend it to parent window

carmine pollen
#

Can you please tell me how to do that. I'm really new to this. And have been trying different ways. I don't know how to send it to parent window now. 😦

#

window.parent.postMessage ?

split panther
#

with making ipc bridge

#

from webview to parent window

carmine pollen
#

How can I achieve this inside my electron app alone, without modifying webview or iframe.

split panther
#

dont know

carmine pollen
#

Ok ..

#

Thank you for thinking through with me.

split panther
#

loading preload file into webview

carmine pollen
#

loading preload file into webview
How is that possible? 🤔

webview is an external URL.

split panther
#

you can load local file into browser window/webview tag as preload file

carmine pollen
split panther
#

Electron's webview tag is based on Chromium's webview, which is undergoing dramatic architectural changes. This impacts the stability of webviews, including rendering, navigation, and event routing. We currently recommend to not use the webview tag and to consider alternatives, like iframe, Electron's BrowserView, or an architecture that avoids ...

carmine pollen
#

Do you know how I can forward the postMessage to parent window?

split panther