Electron Fundamentals, v2 Process Communication Q&A
This course is out of date and does not reflect our standards or industry best practices.
Transcript from the "Process Communication Q&A" Lesson
>> Steve Kinney: Yeah?
>> Speaker 2: So before I knew how to actually do it, I just had made main.js's getFileFromUser function just return the content. And then renderer, it just took the content that was being returned by that function and slapped it in the document the same as we're doing here-
>> Steve Kinney: For an absraction over the same-
>> Speaker 2: And, it worked, but obviously, we want to maybe do this other event-based way. What are the pitfalls of the sort of naive way that I initially did it?
>> Steve Kinney: I don't know if it's necessarily naive. It becomes black magic, [LAUGH] And so part of it, this is how it is working under the hood.
[00:00:45] It becomes a little more interesting though, too, when what happens in one window, you might not need it for that window. So what if it happens if we have a window that is our settings, and changing the settings you need to communicate with a different window. So, generally speaking, returning from the function will work.
[00:01:00] But if you need to get slightly more complicated, then figuring out how to communicate between the different processes and sometimes you're doing a contrived example to show, [LAUGH] How it works under the hood. So I think, yeah, you could totally return out of it, cuz effectively in the same way remote is using IPC, but we don't really see it, right?
[00:01:17] We're just saying, hey, remote don't require main process. Everything's great, right? It's the same basic underlying mechanism both ways.
>> Speaker 2: So, question, all the rendering processes will receive the same message, pretty much?
>> Steve Kinney: So, if we look-
>> Speaker 2: The multiple ones.
>> Steve Kinney: If we look over in the code.
>> Steve Kinney: In this case I'm sending it to the main window, right? So if we had multiple windows, we'd need some kinda data structure, ideally not an array, but let's say like some kind of like map, right? And we'd say, all right, for this Window, I'm gonna send you this message, right?
[00:01:48] And there's all sorts of fun things, like having multiple windows creates a certain number of complexity problems which is, all right, they go in MacOS. They go to the application menu and hit open. Neat, which window, right? On Windows and Linux, the application menu is for every window, right?
[00:02:08] But on MacOS when you have that common one, you need to know what the most recent or the currently focused window is to send the message to. So stuff gets slightly more complicated, right? So for your example, right? For the button, it would work, right? But if we had the application menu, right?
[00:02:23] We're not calling it from the window so we still need to figure out, we'd have to get the focus window and then send an IPC message to it, right? So there's really, to answer the previous question an example where simply returning it wouldn't work. You'd need to find a way to communicate to it.