Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request]: Integrating Node's native MessageChannel Api into Electron #33086

Open
3 tasks done
SimonSiefke opened this issue Feb 25, 2022 · 3 comments
Open
3 tasks done

Comments

@SimonSiefke
Copy link

SimonSiefke commented Feb 25, 2022

Preflight Checklist

Problem Description

For some time now, Node has had the MessageChannel Api.

const { port1, port2 } = new MessageChannel();
port1.on("message", (message) => console.log("received", message));
port2.postMessage({ foo: "bar" });

This Api is very similar - if not exactly the same - as Electrons MessageChannelMain, however currently both have limitations:

Property MessageChannel MessageChannelMain
Port can be transferred to BrowserWindow no yes
Port can be transferred to Worker Thread yes no
Port can be transferred to Child Process no no

Proposed Solution

The proposed solution would be to support Node's native MessageChannel Api, specifically being able to transfer a MessagePort from/to Browser Windows. MessageChannelMain could be deprecated/removed.

Having just one api will greatly simplify development of electron applications:

// Main process
const { port1, port2 } = new MessageChannel()
w.webContents.postMessage('port', null, [port2])
const worker = new Worker("./worker.js");
worker.postMessage(port1, [port1]);


// Renderer process
const { ipcRenderer } = require('electron')
ipcRenderer.on('port', (e) => {
  const port2 = e.ports[0]
  port2.postMessage({ message: 'ping' }) // communicate directly with Worker Process
})


// Worker Process
port1.postMessage({ message: 'pong' })  // communicate directly with BrowserWindow
Property MessageChannel
Port can be transferred to Browser Window yes
Port can be transferred to Worker Thread yes
Port can be transferred to Child Process no

Alternatives Considered

None

Additional Information

No response

@Sankingraitus
Copy link

@sanking

@paralin
Copy link

paralin commented Jun 24, 2022

I guess that transferring a MessageChannel MessagePort to main via a contextBridge function is a no-go today?

https://stackoverflow.com/questions/70786104/

@paralin
Copy link

paralin commented Aug 9, 2022

Transferring MessagePort over the ContextBridge is not supported:

#27024

Transferring MessagePort from preload -> main is supported.
Transferring functions over ContextBridge is supported.

^ Just to clarify to anyone looking at this in future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants