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

Implement full IPC API on render process #73

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

rdickert
Copy link
Contributor

For the app I'm working on, I'd like to be able to communicate arbitrary messages between the main and render processes. It requires a tray, etc., so I'm using a fork of the app/ directory in private/ as recommended in the readme. I think the best way for the main and render processes to communicate is via messages, in the direction suggested in #58, rather than to expose functions directly.

IPC is present in Electron for this purpose, but the full API is currently limited on the render process by meson:electron. Electron.onEvent() allows the render process to receive an IPC message from main, but it does not pass through the event object or any additional parameters you may want to send. Also, there is no way to signal up to the main process.

The code in this PR proposes changing Electron.onEvent() to provide the full IPC API as documented here and adding Electron.sendIpcEvent() for communication from render process to main. I'm not attached to the exact method names. In fact, it might make sense to name them closer to the Electron API: Electron.ipcRendererOn() and Electron.ipcRendererSend() or similar so that new users can relate it to the Electron docs more readily.

These changes make it possible to safely provide any functionality needed. It might make sense to add a method/RPC type of abstraction on top of this where you could provide something like Electron.call('setTrayIcon', 'alert') or Electron.call('promptUser', choices) which could return a promise or otherwise help with async responses. This would feel natural to Meteor devs and wouldn't add much code to meson:electron.

@wearhere
Copy link
Collaborator

Thanks @rdickert! We actually ended up making changes very similar to this in our fork of the preload script, so this is a great contribution to the main script.

* @param {String} event - The name of an event.
* @param {...*} arg - additional arguments to pass to event handler.
*/
sendIpcEvent: function(/* event , ...args */) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind naming this method send? I think that's actually most consistent with the IPC APIs—I should have named the other one just on. I think I just named it onEvent because I was using it more for listening to events than full two-way message passing at that time.

@rdickert
Copy link
Contributor Author

rdickert commented May 11, 2016

@wearhere Good call. Do you want me to also change onEvent() to on() and add an alias for the original with a deprecation warning?

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

Successfully merging this pull request may close these issues.

2 participants