electron: Renderer IPC events in preload script not being delivered
- Electron version: 1.3.2
- Operating system: Windows
Hi All
I have followed the example for ipc communication here but it doesn’t seem to work.
my main process code:
const {ipcMain} = require('electron')
ipcMain.on('asynchronous-message', (event, arg) => {
console.log(arg) // prints "ping"
event.sender.send('asynchronous-reply', 'async pong')
})
ipcMain.on('synchronous-message', (event, arg) => {
console.log(arg) // prints "ping"
event.returnValue = 'sync pong'
})
my browser window code (inside a webview preload script):
const {ipcRenderer} = require('electron')
console.log(ipcRenderer.sendSync('synchronous-message', 'sync ping'))
ipcRenderer.on('asynchronous-reply', (event, arg) => {
console.log(arg)
})
ipcRenderer.send('asynchronous-message', 'async ping')
I get the pings logged:
sync ping async ping
but only the synchronous pong:
sync pong
Is this a bug or am I doing something wrong?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 4
- Comments: 19 (5 by maintainers)
Commits related to this issue
- Add FIXME link to #6828 — committed to electron/electron by kevinsawicki 7 years ago
^^
'did-finish-load'didnt work. Still having the same issue.Thanks @riccardopiola , it was actually my misunderstanding of who will receive the event. I thought it will bubble up in the parent page, not in the main process. It works with both
sendandsendSync.Just for future reference:
sendandsendSyncwill send the message to the main processsendToHostwill send the message to the hostwebviewand can be handled like in this example: https://electron.atom.io/docs/api/webview-tag/#event-ipc-messageCan you test put your async code inside the listener
did-finish-load:Reference: http://electron.atom.io/docs/api/web-contents/#contentssendchannel-arg1-arg2-
@zcbenz any update on this issue?
Hello @stilliard,
I tested with ipcRenderer.on(‘asynchronous-reply’, (arg) => {} Still reproduce the issue. It is reproduced with the following conditions:
I debugged in the code of electron, and think the root cause is that when the message comes back from the other process too fast, document_created_ is false, so electron doesn’t passes the message to the above javascript layer.
void AtomRenderViewObserver::OnBrowserMessage(bool send_to_all, const base::string16& channel, const base::ListValue& args) { if (!document_created_) return;
if (!render_view()->GetWebView()) return; … }
Similar issue here, except for me in the preload script it works if I change:
ipcRenderer.on('asynchronous-reply', (event, arg) => {toipcRenderer.on('asynchronous-reply', (arg) => {Looks like the event isn’t being passed in here, instead just the data of the event. This is on electron v1.4.4 on xubuntu 16.04