Skip to content

Commit 2b46e00

Browse files
Mohammad Hunan Chughtaicbush
andauthored
(DOCSP-11147): Realm Electron Quickstart Doc (#494)
* (DOCSP-11147): Initted Realm Electron Quickstart Doc * finished writing doc for CRA * added electron basic app * added doc about main vs renderer process differences * fix link syntax + wordwrap * added electron code snippets + moved setup into sub header * Update source/node/electron.txt Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron.yaml Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron.yaml Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron-using-create-react-app.yaml Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron-using-create-react-app.yaml Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron-using-create-react-app.yaml Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron.yaml Co-authored-by: Chris Bush <[email protected]> * fix intro + added summary * Update source/includes/steps-realm-with-electron.yaml Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron-using-create-react-app.yaml Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron-using-create-react-app.yaml Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron-using-create-react-app.yaml Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron-using-create-react-app.yaml Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron.yaml Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron.yaml Co-authored-by: Chris Bush <[email protected]> * Update source/node/electron.txt Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron-using-create-react-app.yaml Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron-using-create-react-app.yaml Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron.yaml Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron.yaml Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron.yaml Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron.yaml Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron.yaml Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron.yaml Co-authored-by: Chris Bush <[email protected]> * Update source/node/electron.txt Co-authored-by: Chris Bush <[email protected]> * Update source/node/electron.txt Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron.yaml Co-authored-by: Chris Bush <[email protected]> * fixed wording * fix underlining * fixed grammar mistakes * added additional highlight * Update source/node/electron.txt Co-authored-by: Chris Bush <[email protected]> * Update source/node/electron.txt Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron.yaml Co-authored-by: Chris Bush <[email protected]> * Update source/includes/steps-realm-with-electron-using-create-react-app.yaml Co-authored-by: Chris Bush <[email protected]> * Update source/node/electron.txt Co-authored-by: Chris Bush <[email protected]> * Update source/node/electron.txt Co-authored-by: Chris Bush <[email protected]> * Update source/node/electron.txt Co-authored-by: Chris Bush <[email protected]> * Update source/node/electron.txt Co-authored-by: Chris Bush <[email protected]> * Update source/node/electron.txt Co-authored-by: Chris Bush <[email protected]> * Update source/node/electron.txt Co-authored-by: Chris Bush <[email protected]> * Update source/node/electron.txt Co-authored-by: Chris Bush <[email protected]> * Update source/node/electron.txt Co-authored-by: Chris Bush <[email protected]>
1 parent 68ca744 commit 2b46e00

11 files changed

+493
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const { app, BrowserWindow, ipcMain } = require("electron");
2+
const Realm = require("realm");
3+
4+
function createWindow() {
5+
const mainWindow = new BrowserWindow({
6+
webPreferences: {
7+
nodeIntegration: true,
8+
},
9+
});
10+
11+
mainWindow.loadFile("index.html");
12+
}
13+
14+
app.whenReady().then(() => {
15+
const realmApp = new Realm.App({ appId: "<Your App ID>"}); // Replace <Your App ID> with your application id
16+
let credentials = Realm.Credentials.anonymous();
17+
// log in anonymously
18+
let user = await realmApp.logIn(credentials);
19+
20+
const PersonSchema = {
21+
name: 'Person',
22+
properties: {
23+
_id: 'objectId',
24+
name: 'string'
25+
},
26+
primaryKey: '_id',
27+
};
28+
29+
const config = {
30+
schema: [PersonSchema],
31+
path: "myrealm.realm",
32+
sync: {
33+
user: user,
34+
partitionValue: "My Partition"
35+
}
36+
};
37+
// open a synced realm
38+
39+
const realm = await Realm.open(config)
40+
41+
42+
// when receiving an "asynchronous-message" from the renderer process,
43+
// upload all local changes to the synced realm
44+
45+
ipcMain.on("asynchronous-message", (event, arg) => {
46+
47+
if (arg === "sync") {
48+
console.log("Syncing all local changes");
49+
50+
realm.syncSession.uploadAllLocalChanges();
51+
52+
}
53+
});
54+
55+
createWindow();
56+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const { ipcRenderer } = require("electron");
2+
const Realm = require("realm");
3+
const ObjectId = require("bson").ObjectId;
4+
5+
async function run() {
6+
const realmApp = new Realm.App({ id: "<Your App ID>" }); // Replace <Your App ID> with your application id
7+
let credentials = Realm.Credentials.anonymous();
8+
// log in anonymously
9+
let user = await realmApp.logIn(credentials);
10+
11+
var PersonSchema = {
12+
name: "Person",
13+
properties: {
14+
_id: "objectId",
15+
name: "string",
16+
},
17+
primaryKey: "_id",
18+
};
19+
20+
const config = {
21+
path: "myrealm.realm",
22+
schema: [PersonSchema],
23+
sync: true,
24+
};
25+
26+
// open a non synced realm
27+
28+
const realm = new Realm(config);
29+
30+
const personList = realm.objects("Person");
31+
32+
// create a new "Person"
33+
realm.write(() => {
34+
john = realm.create("Person", {
35+
_id: new ObjectId(),
36+
name: "John Smith",
37+
});
38+
});
39+
40+
// Sending a request for sync to main
41+
42+
ipcRenderer.send("asynchronous-message", "sync");
43+
}
44+
45+
run();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const electron = require("electron");
2+
const Realm = require("realm");
3+
4+
const { app, BrowserWindow } = electron;
5+
6+
function createWindow() {
7+
// Create the browser window.
8+
const win = new BrowserWindow({
9+
width: 800,
10+
height: 600,
11+
webPreferences: {
12+
nodeIntegration: true,
13+
},
14+
});
15+
16+
// and load the index.html of the app.
17+
win.loadFile("index.html");
18+
}
19+
20+
app.whenReady().then(createWindow);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const nodeExternals = require("webpack-node-externals");
2+
3+
module.exports = {
4+
webpack: {
5+
configure: {
6+
target: "electron-renderer",
7+
externals: [
8+
nodeExternals({
9+
allowlist: [/webpack(\/.*)?/, "electron-devtools-installer"],
10+
}),
11+
],
12+
},
13+
},
14+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const electron = require("electron");
2+
const path = require("path");
3+
4+
const app = electron.app;
5+
const BrowserWindow = electron.BrowserWindow;
6+
7+
let mainWindow;
8+
9+
function createWindow() {
10+
// Create the browser window.
11+
mainWindow = new BrowserWindow({
12+
width: 800,
13+
height: 600,
14+
webPreferences: { nodeIntegration: true },
15+
});
16+
// and load the index.html of the app.
17+
console.log(__dirname);
18+
mainWindow.loadFile(path.join(__dirname, "../build/index.html"));
19+
}
20+
21+
// This method will be called when Electron has finished
22+
// initialization and is ready to create browser windows.
23+
// Some APIs can only be used after this event occurs.
24+
app.on("ready", createWindow);

source/images/electron-app.png

80.2 KB
Loading

source/images/electron-cra.png

148 KB
Loading
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
title: Create Your React Application
2+
ref: create-your-react-application
3+
content: |
4+
Scaffold your application by using the `Create React App
5+
<https://reactjs.org/docs/create-a-new-react-app.html>`_ toolchain.
6+
Enter the following command into your terminal:
7+
8+
.. code-block:: shell
9+
10+
npx create-react-app my_electron_react_application
11+
12+
---
13+
title: Install CRACO to Alter Your Webpack Configuration
14+
ref: install-craco
15+
content: |
16+
To allow your application to work properly with Electron, you have to
17+
alter your webpack configuration. By default, applications created via
18+
create-react-app use a preconfigured webpack file and hidden to the
19+
end-user. The Create React App default webpack configuration is not
20+
compatible with {+service+} and you must override it. Using `CRACO
21+
<https://www.npmjs.com/package/@craco/craco>`_ you can override these
22+
default configurated values. Install CRACO with the following command:
23+
24+
.. code-block:: shell
25+
26+
npm install @craco/craco
27+
28+
---
29+
title: Create a CRACO Configuration File
30+
ref: create-craco-config
31+
content: |
32+
In order to override the preconfigured webpack values, create a CRACO
33+
config file called ``craco.config.js`` at the root of your
34+
application. Add the following to this file:
35+
36+
.. literalinclude:: /examples/QuickStarts/electron/quickstart-setup-react-cracoconfig.js
37+
38+
The `target <https://webpack.js.org/configuration/target/>`_
39+
is set to "electron-renderer" to compile your application for browser
40+
environments for Electron built-in modules. ``nodeExternals`` is also
41+
specified to prevent all node_modules from being bundled. The
42+
``allowList`` key in the object passed in to ``nodeExternals``
43+
specifies a list of modules to include in the bundle. Here, webpack
44+
and the electron-devtools are included in the bundle. In order to make
45+
use of webpack-node-externals, run the following command:
46+
47+
.. code-block:: shell
48+
49+
npm install webpack-node-externals --save-dev
50+
51+
---
52+
title: Install Electron
53+
ref: install-electron
54+
content: |
55+
To add Electron to your project, run the following command:
56+
57+
.. code-block:: shell
58+
59+
npm install electron --save-dev
60+
61+
---
62+
title: Create Your Electron Main Process File
63+
ref: create-your-electron-main-process
64+
content: |
65+
The Electron ``main process`` file can be thought of as the entry
66+
point into your application. This file is responsible for loading your
67+
React App's ``index.html`` file into the ``BrowserWindow`` created by
68+
Electron.
69+
70+
.. note::
71+
Each electron application can only have one **main process**. The
72+
main process can create web pages. Each web page runs in its
73+
own process, known as a **renderer process**. To learn more about
74+
this, read the official `Electron Application
75+
Architecture <https://www.electronjs.org/docs/tutorial/application-architecture>`_
76+
doc.
77+
78+
Add the following code to a new file called ``electron.js`` in the
79+
``public`` directory:
80+
81+
.. literalinclude:: /examples/QuickStarts/electron/quickstart-setup-react-electron.js
82+
83+
---
84+
title: Run Your Application
85+
ref: run-your-app
86+
content: |
87+
In order to run your application, specify a homepage and a main entry point for Electron by adding the following to your ``package.json`` file:
88+
89+
.. code-block:: shell
90+
91+
"main": "public/electron.js",
92+
"homepage": "./",
93+
94+
Finally, add the following scripts to your ``package.json`` file:
95+
96+
.. code-block:: shell
97+
98+
"scripts": {
99+
"build": "craco build",
100+
"start": "electron ."
101+
},
102+
103+
In your terminal, run ``npm run build`` in one tab and ``npm run
104+
start`` in another tab. You should see the following:
105+
106+
.. cssclass:: bordered-figure
107+
.. figure:: /images/electron-cra.png
108+
:alt: Electron Desktop App with React
109+
110+
---
111+
title: Install Realm
112+
ref: install-realm
113+
content: |
114+
In your terminal use the following command to add Realm to your project:
115+
116+
.. code-block:: shell
117+
118+
npm install realm@beta
119+
120+
You can now begin using Realm in either the renderer processes or the
121+
main process.
122+
123+
Use realm in the renderer process by adding the following to the top
124+
of the ``src/App.js`` file (you will also need to import it in whichever
125+
file you write code using Realm in):
126+
127+
.. code-block:: shell
128+
129+
import Realm from "realm";
130+
131+
Use Realm in the main process by adding the following to the top of the ``public/electron.js`` file:
132+
133+
.. code-block:: shell
134+
135+
const Realm = require("realm");
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
title: Set up Your Application Files
2+
ref: setup-application-files
3+
content: |
4+
To begin developing your application, create an application directory:
5+
6+
.. code-block:: shell
7+
8+
mkdir myElectronApplication
9+
10+
Create an index.html and main.js file in the root directory of
11+
your application. The code in the main.js file is the Electron main
12+
process file, which can be thought of as the entry point into your
13+
application. The main.js file is responsible for loading your
14+
index.html file into the BrowserWindow created by Electron.
15+
16+
.. note::
17+
18+
Each Electron application can only have one **main process**. The
19+
main process creates web pages. Each web page runs in its
20+
own process, known as a ``renderer process``. To learn more about
21+
this, read the official `Electron Application
22+
Architecture <https://www.electronjs.org/docs/tutorial/application-architecture>`_
23+
doc.
24+
---
25+
title: Set up a ``package.json``
26+
ref: setup-packagejson
27+
content: |
28+
Initialize a package.json to begin installing packages and using them
29+
in your project. Run the following command in your terminal:
30+
31+
.. code-block:: shell
32+
33+
npm init -y
34+
---
35+
title: Install Your Project Dependencies
36+
ref: install-dependencies
37+
content: |
38+
In order to begin developing your Electron Application that uses
39+
Realm, install the necessary dependencies. Install electron as a dev
40+
dependency by running the following command:
41+
42+
.. code-block:: shell
43+
44+
npm install electron --save-dev
45+
46+
Install Realm by running the following command:
47+
48+
.. code-block:: shell
49+
50+
npm install realm@beta
51+
52+
---
53+
title: Create a Script to Run Your Application
54+
ref: create-script-to-run-app
55+
content: |
56+
Add a script to your ``package.json`` file to run your application.
57+
58+
.. code-block:: json
59+
60+
"scripts": {
61+
"start": "electron ."
62+
}
63+
64+
Running the command ``npm start`` will now start your Electron app.
65+
---
66+
title: Create a Browser Window Using Your Main Process
67+
ref: create-browser-window
68+
content: |
69+
In order to display HTML to the users, your application needs a
70+
browser window. Create a `browser window
71+
<https://www.electronjs.org/docs/api/browser-window>`_ using the
72+
Electron API. Copy the following code into your ``main.js`` file:
73+
74+
.. literalinclude:: /examples/QuickStarts/electron/quickstart-setup-main.js
75+
:language: javascript
76+
:emphasize-lines: 2, 8
77+
:linenos:
78+
79+
Notice the ``require("realm")`` on line 2. This line makes Realm
80+
accessible in this main process file.
81+
---
82+
title: Run Your Application
83+
ref: run-electron-app
84+
content: |
85+
In order to run your application, enter the following into your terminal:
86+
87+
.. code-block:: shell
88+
89+
npm start
90+
91+
You should see the following:
92+
93+
.. cssclass:: bordered-figure
94+
.. figure:: /images/electron-app.png
95+
:alt: Electron Desktop App

0 commit comments

Comments
 (0)