Interacting with Realm from an Electron renderer process - IPC or not?

The Realm docs encourage you to load Realm in an Electron renderer process. But I struggled to get this working.

And anyway, the it feels from Electron documentation that disk-accessing functions should be in the main process instead sending/receiving messages through the IPC bus to the renderer.

The benefits:

  • Conforms better to Electron’s security model
  • No jumping through hoops to get Realm loaded in the renderer process which I still haven’t figured out how to do without errors

Downsides I’ve found:

  • Can’t send realm objects through the IPC bus - they must be serialised as plain JSON, losing any live updating benefits - but can be mitigated by sending change events instead, I believe
  • Not how the documentation is written, so it’s hard to know good patterns to follow

Does anyone have a working example of using Realm through the IPC? Or is it still recommended to load it directly in the renderer process?

I’m aware of this Advanced Quickstart linked in the docs that illustrates how to use realm from both main and renderer processes, but it’s two years old and probably predates IPC so I suspect it’s not the best practice any more.

Right now our recommended approach still stands and is the only way to go. We don’t have a recommendation for how to integrate with IPC, the only way to do things today is to bypass it, which we know is against the security recommendations. In the coming quarter we will be working on multi-process for sync realms which will enable a more elegant integration with Electron, once this ships we will re-evaluate our recommended integration with Electron.

For now though, you can certainly share the error you are getting when trying to integrate using our recommended approach and we can try to help.

Thanks for the confirmation Ian. FYI I’m using Electron 20.0.2, which I know the docs are not written for.

Electron React Boilerplate

I am trying to use Electron React Boilerplate which is not based on create-react-app, so the official Realm instructions don’t help much. According to the ERB instructions for adding dependencies I believe Realm should be added to the deeper package.json file in release/app (which should take care of the ‘externals’ concept the the realm docs refer to). But Realm remains undefined in my renderer process. even after running npm install at both levels so something’s going wrong and I’m not sure what.

Create React App

For comparison I’ve now tried again using create-react-app, as the Realm docs describe, and I’m able to get Roam loaded from the renderer process successfully when running in dev mode.

However, when packaging it I ran into this problem with the mongodb-realm dir being created in the wrong place and so now I need to figure out how to solve that with the newer Electron version, because the solution described there I think relies on electron.remote which is not advised any more - but I haven’t tried yet to figure that out.

Hmmm - I wonder if you set this parameter here -

To explain: realm with sync is single-process (although we are building multiprocess now) which is why we set these different parameters between main vs renderer in an Electron app today as our recommendation.

Thanks, I’ll try again some time. I was just experimenting for now.

Haven’t tested thoroughly, but looks like I can put: process.chdir(app.getPath('userData')); in my main.js file, even if I’m requiring realm from the renderer. The renderer can’t access the app object directly but since we’re only changing directories this can be done from the main process instead.

It feels a bit weird that Realm creates this directory when you require it rather than giving you a nice moment to set the correct directory before you actually open a realm.