Using grpc from Electron / Node main and renderer...

57 views
Skip to first unread message

Kevin Burton

unread,
Sep 7, 2018, 9:56:18 AM9/7/18
to grpc.io
So I think grpc could be used to help out with a major issue for Electron apps.

Essentially the main process can be a bottleneck around the UI thread.  You basically don't want to lock it up as the IPC can't be used to control the browser process. 

Additionally, there's the issue of blocking the UI thread with computationally complex activities.

On top of that there's an issue of needing a centralized coordinator for some activities like locking, updating files on disk , etc.  

We could use grpc to solve these problems I think.

The grpc daemon could sit outside the main electron and render processes and allow all components to talk to each other and discover each other.

It *looks* like grpc-web could solve this but I'm unclear if it works in the renderer process.

This would save me a huge amount of time if it were possible to get to work within Electron.

Thoughts?

Michael Lumish

unread,
Sep 7, 2018, 1:06:49 PM9/7/18
to Kevin Burton, grpc.io
Before I address this question I want to be sure that we have the same understanding of how Electron works at a low level. Electron is a single process with a single thread. It contains two separate sets of JavaScript execution state. One is a Chromium environment that is called the "renderer process", and the other is a Node environment that is called the "main process". These are correspondingly backed by a Chromium event loop and a Node event loop, which the single real process executes in alternation. The IPC system is really just an in-process message passing system for moving data between those two JS execution environments.

It is true that either part of the process can bottleneck the other, because this is a cooperative multitasking environment. The best way to avoid blocking the UI when you need to do something computationally expensive is to break it down into steps and perform the steps asynchronously, to give the UI event loop a chance to run.

Given that information, gRPC does not seem to solve most of those problems. You could use it to offload computationally expensive work to somewhere else, like with any other client/server setup.

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/7ebd434a-d64d-4600-a04f-a1de28dd4407%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nicolas Noble

unread,
Sep 7, 2018, 1:28:33 PM9/7/18
to Michael Lumish, burto...@gmail.com, grpc.io
I'd add that Electron allows you to call any node module from the renderer process, so it's possible to require('grpc') from within your renderer javascript code.

Kevin Burton

unread,
Sep 7, 2018, 2:31:58 PM9/7/18
to grpc.io
Yes.. I think I can just require grpc... 

I think in that sense if I can then it should work due to 'node integration'.  

This might 'just work' out of the box then considering that I could use grpc directly. 

Kevin Burton

unread,
Sep 7, 2018, 2:32:58 PM9/7/18
to grpc.io

Given that information, gRPC does not seem to solve most of those problems. You could use it to offload computationally expensive work to somewhere else, like with any other client/server setup.


I will have to look into the gRPC internals but I assumed that starting it would yield its own thread and event loop.  

I would probably want to have one thread per IO subsystem though. 

also, this way I can rationalize about IO since I'm just in a single thread.   

Michael Lumish

unread,
Sep 7, 2018, 2:35:48 PM9/7/18
to Kevin Burton, grpc.io
gRPC on Node uses the Node event loop, and it does all of its work on the main thread.

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.

Kevin Burton

unread,
Sep 7, 2018, 5:01:10 PM9/7/18
to grpc.io
Ah.. ok.  that sort of defeats the whole point. There's now way to make it threaded?  

I was reading in the documentation about ThreadManager and how it uses a thread pool. I was just planning on configuring this for 1 

Kevin Burton

unread,
Sep 7, 2018, 5:03:30 PM9/7/18
to grpc.io
I worked on a minimum proof of concept and it looks like grpc requires native binaries for electron.  

Though I can't seem to find any.  Rebuilding for all platforms would be very time consuming.

Michael Lumish

unread,
Sep 7, 2018, 5:15:32 PM9/7/18
to Kevin Burton, grpc.io
If you want to use gRPC with electron you have to pass some extra arguments at installation time to indicate that you want the electron binaries. So it would look something like this: npm install grpc --runtime=electron --target=<electron version>. Just replace "<electron version>" with whatever version of electron you are using.

Kevin Burton

unread,
Sep 7, 2018, 6:33:24 PM9/7/18
to grpc.io
Ah.. nice.. is that documented anywhere?  

Michael Lumish

unread,
Sep 7, 2018, 6:39:56 PM9/7/18
to Kevin Burton, grpc.io
Yes, the README has a section about it. You can find that on the npm package page, and in the repository.

Reply all
Reply to author
Forward
0 new messages