<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Wykrhm Reddy on Medium]]></title>
        <description><![CDATA[Stories by Wykrhm Reddy on Medium]]></description>
        <link>https://medium.com/@wykrhm?source=rss-e534595de09b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*5xSpKO4K0brpL-VvLjJ9Ww.jpeg</url>
            <title>Stories by Wykrhm Reddy on Medium</title>
            <link>https://medium.com/@wykrhm?source=rss-e534595de09b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 13:03:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@wykrhm/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Creating standalone Desktop Applications with React, Electron and SQLite3]]></title>
            <link>https://wykrhm.medium.com/creating-standalone-desktop-applications-with-react-electron-and-sqlite3-269dbb310aee?source=rss-e534595de09b------2</link>
            <guid isPermaLink="false">https://medium.com/p/269dbb310aee</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[electron]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[sqlite]]></category>
            <category><![CDATA[blender]]></category>
            <dc:creator><![CDATA[Wykrhm Reddy]]></dc:creator>
            <pubDate>Fri, 16 Oct 2020 04:28:26 GMT</pubDate>
            <atom:updated>2021-06-23T18:53:01.808Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gCx7XwM2YOi1G98EX32fjQ.png" /></figure><p>There are quite a few tutorials on the Internet that cover the process of setting up React inside an Electron app but very few (if any) cover the solutions to common problems you run in to when packaging the app for distribution. In this blog, I am going to run you through how I setup a production ready work flow for creating a desktop app with <strong>React </strong>in <strong>Electron</strong> and <strong>SQLite3 </strong>as a database packaged with the application.</p><p>In my most recent personal project, I built a Daily Build Updater for the 3D application <a href="https://www.blender.org/">Blender</a>. This retrieves the latest daily build, compares it with your existing installation, lets you update or reinstall, clean up older installations and setup your shortcuts. A one click solution to automate the entire process. You can check out a preview of the app below.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FnGbL1dEaph8%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DnGbL1dEaph8&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FnGbL1dEaph8%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/c033023cf8d3892a1d73a4e131c35701/href">https://medium.com/media/c033023cf8d3892a1d73a4e131c35701/href</a></iframe><p>Let’s get started.</p><p>First and foremost, let us get React. The simplest and best way to do it would be using the <strong>Create React App</strong>. I will be using <strong>yarn</strong>. You can use <strong>npm</strong>.</p><pre>yarn create react-app MyDesktopApp<br>cd MyDesktopApp</pre><p>Now that we have React ready to go, let’s setup Electron. We’ll need to install a few packages for that.</p><p><strong>Development Dependencies:</strong></p><ul><li>electron — <em>well .. Electron.</em></li><li>electron-builder — <em>To package the Electron app.</em></li><li>nodemon — <em>To monitor for changes during development and hot reload.</em></li><li>concurrently — <em>To launch the React app and Electron together</em></li><li>wait-on — <em>To wait for the React app to launch before the Electron app is launched.</em></li></ul><p><strong>Dependencies:</strong></p><ul><li>electron-is-dev — <em>Simple library to check if we’re in development mode or not.</em></li><li>sqlite3 — <em>We’ll use it later when we implement our database.</em></li></ul><pre>yarn add electron electron-builder nodemon concurrently wait-on -D<br>yarn add electron-is-dev</pre><p>We have the packages we need. We can get started with setting up Electron.</p><p>In your <strong>public</strong> folder, create a two new files called <strong>electron.js </strong>and <strong>preload.js</strong>. If you care to know, the reason we are creating it in this folder specifically is because when you build the React app, all contents of this folder get carried over to the <em>build</em> folder which makes things very simple for production. Additionally, any changes to files in this folder will trigger hot reload during development which can be very handy considering changes to the base Electron setup requires a full relaunch anyway.</p><p>The <strong>electron.js</strong> file will have the following code. It’s <a href="https://www.electronjs.org/docs/tutorial/quick-start">basic Electron setup</a> but I’ll add comments where explanation is necessary.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72d99bd817b213f4e539559953eed215/href">https://medium.com/media/72d99bd817b213f4e539559953eed215/href</a></iframe><p>Now that we have defined the Electron window, let us test out the app by booting it in development mode. Go to <strong>package.json</strong> and add the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*07zAjaZfc7INZkJQ_NsdSQ.png" /></figure><p>By setting main, you are letting Electron know where to get the info for the window from and by setting homepage, you’re letting React know to build the app keeping in mind that the base location is the default location — <em>This is required if your front is going to be a single page application</em>.</p><p>Now we need a few <strong>scripts </strong>in<strong> package.json</strong> to be changed and added. Rename “start” to “<strong>start-react</strong>” and build to “<strong>build-react</strong>” and then add the following. Your scripts should look something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wW1wzTDv5oZJU72AQhqcmg.png" /></figure><p>To explain what they do —</p><ul><li>start-react — <em>Will start just the React app only</em></li><li>build-react — <em>Will build the React app only</em></li><li>start-electron — <em>This will use nodemon to watch for changes in the public folder and then execute electron. </em><strong><em>If you want to add more folders to be monitored, just add another </em></strong><strong><em>—-watch followed by the path to that folder</em></strong><em>.</em></li><li>dev — <em>Will first run React, wait for it to boot up and then start Electron.</em></li><li>postinstall — <em>It will make electron-builder install any dependencies we need for our app</em></li><li>pack-app — <em>Building the app can take time. Packing the app is shorter. It’ll just pack the app so you can test your production builds.</em></li><li>build- <em>Builds your app for distribution</em></li><li>test — <em>Comes with Create React App. Testing for your React app.</em></li><li>eject — <em>Comes with Create React App. Ejects your app from the CRA pipeline.</em></li></ul><p>Now just do yarn dev and it should boot up your React app inside an Electron Window.</p><p>Now that we have our development phase setup. Let us also set our build settings for production.</p><p>We need to add a <strong>build </strong>category to our <strong>package.json</strong> for <strong>electron-builder</strong> to know how we want our app built. There’s <a href="https://www.electron.build/configuration/configuration"><strong>tons of options</strong></a> but here’s some basic ones for a Windows app. Refer to the link above for the full electron-builder documentation which is pretty clear.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*duNKUtR22s3Nw67KNKGFpg.png" /></figure><p>To explain the above code — <strong>UNDERSTAND THIS</strong> — it’ll make your life a lot easier distributing the product -</p><ul><li>build category — <em>Needed to provide information to electron-builder</em></li><li>appId — <em>Has to be unique so your app can be identifiable.</em></li><li>productName — <em>Name of your app</em></li><li>copyright — <em>Your app copyright info</em></li><li>files — <em>All the files you want to be packed in to the archives (asar) by electron-builder. **/* indicates every file inside a particular folder. In this case, I’m packing the entire </em><strong><em>build </em></strong><em>folder.</em></li><li>directories — <em>The location where electron-builder has to look for resources it needs. In our case, it is the build folder.</em></li><li>extraResources —<em> So here’s the tricky part.<br>Along with the stuff you pack in the files category, you can pack extra files and folders with your application. These </em><strong><em>won’t</em></strong><em> be packed in the archive(asar) but rather get </em><strong><em>copied</em></strong><em> in the </em><strong><em>resources </em></strong><em>folder of the application.</em><br><strong><em>So which files to add here?</em></strong><br><em>The general rule I follow is — if my app needs to edit or update info inside a particular file, then it needs to be an extra resource. If it doesn’t need to, then it can go inside the file category above. In the above example, you can see a template on how to add individual files and also entire folders.<br>If you are using </em><strong><em>worker_threads</em></strong><em>, then you need to pack your worker thread file as an extraResource too.<br>If you are going to add a file as extraResources, then you can access that in your code with </em><strong><em>process.resourcesPath</em></strong><em> which gives you the direct path to the resources folder. So in my case, if Iwant to access the db folder, it just type path.join(process.resourcesPath, “db/filename.db”) for the production build.</em></li><li>win — <em>The win category contains instructions specific to a windows build. You add a category called mac if you want to do a mac and etc. Follow the electron-builder docs for info on this.</em></li></ul><h3>Communication Between React &amp; Electron</h3><p>So the important part. How do we communicate between React and Electron? Most tutorials online would recommend that you <em>enableNodeIntegration</em> on your Electron app and use the <em>ipcRenderer</em> / <em>ipcMain </em>directly to talk to your frontend.</p><p>But it is <strong>NOT </strong>safe to expose your Electron backend to the renderer. The same issue even persists with the usage of Electron <strong>remote</strong>. Electron themselves have recommended against this and the remote module has been deprecated in Electron 12 and will be removed in Electron 14.</p><p>So what is the solution? Well, that’s where the <strong>preload.js</strong> which we created and the <strong>contextIsolation: true</strong> web preference we set earlier come in.</p><p>Firstly, the contextIsolation. By enabling this, you’re isolating the Electron logic to run in a separate context to that of the front end you load. This will ensure that any rogue scripts do not have access to the all powerful Electron backend. But obviously, this would cut off the frontends access to the Electron backend.</p><p>So in order for these two sides to be able to talk to each other, we create a <strong>bridge</strong>. That might sound complicated but it really is not.</p><p>What you are doing is basically creating a simple API inside your preload.js by defining simple functions that are preloaded to the app. So the front end only has access to these functions only making your app secure.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e42308ee33afcc748981d8454e21f6da/href">https://medium.com/media/e42308ee33afcc748981d8454e21f6da/href</a></iframe><p>In the above file, we loaded ipcRenderer and contextBridge from electron.</p><p>ipcRenderer will let us send signals which we will catch with ipcMain in electron.js.</p><p>contextBridge is basically creating a bridge between React and Electron with the already pre-defined functions that you’ve exposed to the front end. So React can only access these. Nothing else.</p><p>Keys used like ‘test-invoke’, ‘test-send’ and etc need to be <strong>unique </strong>so there’s no clashes in the signals sent.</p><p>So instead of using ipcRenderer directly in React, you will now use <strong>window.api.testInvoke(‘example argument) </strong>which will trigger <strong>ipcRenderer.invoke(‘test-invoke’, ‘example argument’)</strong> that you might be familiar with.</p><p>There’s 3 basic ways of communication — I’ll provide examples right after:</p><ol><li><strong>invoke </strong>— You send data from the frontend, process it with <strong>ipcMain.handle</strong> on the backend and return information to the frontend.</li><li><strong>send </strong>— You send data from the frontend, process it in the backend with <strong>ipcMain.on</strong> and send back a reply when it is processed.</li><li><strong>on</strong> — You receive data from the backend <strong>event.sender</strong> and process that with the help of a callback function.</li></ol><p><em>There is no functional difference between </em><strong><em>invoke </em></strong><em>and </em><strong><em>send</em></strong><em> as far as I know. invoke is a newer API and uses Promises. So I use invoke for Promise based operations and send for others. If there is something I am missing here, then please feel free to let me know.</em></p><h3><strong>EXAMPLES</strong>:</h3><p>Let’s say I want to enter username, password in React, process that info in Electron / Node and get back my profile information.</p><p><strong>INVOKE</strong></p><p><strong>In preload.js</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Begm1ECPG38_w8p50_gvzA.png" /></figure><p><strong>In ReactComponent.js</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GoyoL9zgZZRvOhIgWp5hIA.png" /></figure><p><strong>In electron.js</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M9Xtsd1llT0Y61LutbpqIw.png" /></figure><p>In the above example, when the user submits the form, the formHandler<strong> </strong>sends the form data via the windows.api.getProfileInfo. The getProfileInfo<strong> </strong>function we set in the preload will trigger the get-profile-details signal. The get-profile-details signal is caught in the electron.js file where the arguments are received, data is retrieved from the database, verified and then profile info is returned. This returned profile info is awaited for in the React app and if its not null, it is set to the React state.</p><p><strong>SEND</strong></p><p><strong>In preload.js</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3bYeRe3s4TfQ5nYTU-xXBw.png" /></figure><p><strong>In ReactComponent.js</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ycFvaqN8xf3GwPwDINAo-g.png" /></figure><p><strong>In electron.js</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FoQJfNmiQCFx8WrpwLn48g.png" /></figure><p>Trigger quitApp() from the renderer. Quit the app using electron.js — Send a reply for funsies because who cares once the app is closed.</p><p><strong>RECEIVE</strong></p><p>In <strong>preload.js</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P8EHQa-DIKdsqayaZ26XXA.png" /></figure><p>In <strong>electron.js</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9nnrtFYqtetDHdVHYP2lmQ.png" /></figure><p>In <strong>ReactComponent.js</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mXeAIBIS_sjuZXJaiK7l2A.png" /></figure><p>In this example, we click a button which triggers the <strong>downloadFileHandler </strong>which triggers the <strong>downloadFile </strong>signal passing the data from the form.</p><p>The <strong>downloadFile </strong>signal sent from <strong>preload.js </strong>with the data is caught by the electron.js file where the data is sent to an imaginary module called ‘<em>filedownloader</em>’ whose function ‘<em>DownloadFile</em>’ has a callback function for the progress called <em>onProgress</em>.</p><p>onProgress send back the progress info through an event sender called ‘<strong>get-download-progress</strong>’ which is caught by <strong>getDownloadProgress </strong>in <strong>preload.js.</strong> The ReactComponent is constantly listening to getDownloadProgress with <strong>useEffect </strong>and updating the progress data each time it updates to the state.</p><h3>Adding a Database</h3><p>We will be using SQLite3 for this particular application because we can pack it along with the app.</p><p>Use <a href="https://sqlitebrowser.org/">DB Browser for SQLite</a> to create a new database. You can store it anywhere. I’d recommend creating a folder called <strong>db</strong> in your root folder.</p><p><strong>NOTE: Make sure </strong>you add this folder to the extraResources path in the build category. Because we want to ship our database file along with our application.</p><p>We’ll be using Node.js’ sqlite3 to communicate with our database.</p><pre>yarn add sqlite3</pre><p>sqlite3 generally needs bindings to be rebuilt. Because we have <strong>postinstall </strong>for electron-builder already setup, installing it right now should have fixed that issue for us. But if it did not, then just run yarn postinstall and watch for the success message.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41a117e5b2de6124240f22621b9afbd3/href">https://medium.com/media/41a117e5b2de6124240f22621b9afbd3/href</a></iframe><p>You can refer to the <a href="https://www.sqlitetutorial.net/sqlite-nodejs/"><strong>SQLite documentation</strong></a> on how to use it. It’s pretty straight forward.</p><h3><strong>Conclusion</strong></h3><p>That’s it. When you’re done with your app, do a yarn pack-app to <strong>pack </strong>and test or yarn build to <strong>build </strong>it for distribution.</p><p>Let me know if I buggered up anything anywhere and feel free to buzz me up for any queries. You can catch me on Twitter at <a href="http://www.twitter.com/wykrhm">@wykrhm</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=269dbb310aee" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>