electron-webapps / meteor-electron

Meteor Electron, the easiest way to create a desktop Meteor application
MIT License
325 stars 46 forks source link

Implement full IPC API on render process #73

Open rdickert opened 8 years ago

rdickert commented 8 years ago

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 commented 8 years ago

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.

rdickert commented 8 years ago

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