What Is Electron and Why Should We Use it?
What Is Electron and Why Should We Use it?
In this post, we'll analyze the issues with the current way of developing desktop applications, and introduce a use case we'll cover in future articles.
Join the DZone community and get the full member experience.Join For Free
Learn how error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.
Written by Sai Sarath Chandra, Alibaba Cloud Tech Share author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.
Before we talk about Electron, we need to take a step back and see how desktop applications are made. We will also analyze the issues with the current way of developing desktop applications. After that, we will explore how to create a fully functional invoice application backend using Electron.
App Development on Desktops
Let's talk about how Windows-based apps are created. Most people use C++, C# (C Sharp), VB (Visual Basic) for development. This may include using/reusing some open source .NET libraries to make applications across all the windows ecosystem like Windows store, windows phone, windows desktop application.
On Mac, we have a clear abstraction of technologies we use to create applications. We have the "Cocoa" layer, which includes all the technologies we require to create the application-user interface. The "Media" layer has all the tools and technologies required for the Media operations which include the 2D and 3D animations, Photo and Video editing.
There is also a "Core Services" area where we can perform all the low-level network operations, and string and data manipulation. "Core OS" exposes all the API/provides functionality for all the CPU and GPU to perform high-performance tasks. "Kernel and Device Drivers" provide support for file systems, networking, security, inter-process communication, programming languages, device drivers, and extensions to the kernel. This level also exposes the Mach kernel environment, device drivers, BSD library functions (libSystem), and other low-level components
As you can see, there is a lot you need to learn about the technologies and frameworks in order to develop apps on MacOS. Note that there is no similarity with the Windows technology stack we are using.
Linux is one of the most popular OSs for developers, so we want our products to reach Linux users. I will be talking about both Ubuntu and Linux together. Linux typically uses Python or other libraries for the user interface of applications. For Linux, if you need services like 3D and 2D rendering, you will need OpenGL. Linux Kernel exposes the low-level functionalities.
What Is it With Web Technologies?
Web development is one of the hottest topics out there and it is no surprise that the population of web developers is rather high. There are also a lot of web developers available who can design and create apps with amazing UI/UX. This is because these technologies are standard and are being used by top companies in the world for their production projects. There are innumerable frameworks out there helping other teams, backed by tech giants in the industry.
In the fast-paced world of web development, businesses need to cope with the rapid and constant changes in the tech industry. This means your product needs to be prototyped faster and available for all platforms, as well as with a single codebase, support for more features, and great UI/UX. If we just depend on conventional methods, we need a lot of capital and time to achieve this. Instead, if we use web technologies, we can reuse a lot of open source frameworks and solve issues efficiently.
Electron: What it Is and Why Use it
Electron helps us develop cross-platform applications by using existing web technologies. You don't need specific skills, for most cases, to develop apps with Electron. If you are a web developer and are interested in developing an app with Electron, then make sure you check out tutorials that will follow this article in this series.
You don’t need to think much when migrating your existing application to Electron as the application we are creating is a desktop application and data stays locally in the system. Because of this, you can ensure data security. In case you need to store data in the cloud, check whether your cloud network has enough security functions beforehand to avoid any unwanted surprises.
Before starting out, you also have to check whether all the functionalities you are providing for your desktop application are also available in Electron. In my experience, Electron provides enough control to have the extended interactive features in your application, such as Keyboard Shortcuts. It also provides low-level accessibility to the hardware and operations system components.
Electron thrives in this aspect. If proper care is taken while developing (load only what you need), Electron can show some great gains in the terms of performance when compared to native applications. Electron saves a lot of time and provides more options to play with or develop by having a single code base for all the major platforms. These are the major problems when we deal with the native development of applications, which are all effectively solved by Electron.
However, a lot of people say that Electron is very memory hungry. I agree with this statement, but only if you develop the applications without proper care.
Code and App Management
As a Product Owner, you don’t need to maintain different teams for each platform and you will be relieved from the hassle of re-explaining your requirements with different teams. It will also reduce the auditing work to make sure the product has the same functionality across platforms.
As a developer, you need not worry about different codebases. If you encounter a bug on any platform, you can fix it at the code base. The bug will never show up on other platforms. However, you should still keep an eye on the OS level functionalities.
Because we are using a single code base, it means we can use this for both web applications and desktop applications. We are, in a way, also reusing the base code across different platforms since we “code once, distribute everywhere.”
The more known frameworks we use, the more support we get. This, in turn, gives us more open source libraries we can reuse and the decreased time to production but with more features.
This is one of the interesting aspects of Electron. There is an electron-packager module available that helps us bundle the whole code base into the respective packages. Although people debate that Electron takes a lot of memory, as I said, Electron needs a little care during development to avoid this.
With web technologies, you are open to multiple technologies that provide great User Interface (UI) and User Experience (UX) to all your users with great comfort. Also, you can be sure that you are providing the same experience to all your users across different platforms.
Cost and Time
You will save a lot of development time and money because, for the tech stack we use, there are a lot of developers who can do it for less and achieve good results. You can save a lot of time using the single code base and any developer can work on anything.
History of Electron
When we talk about any technology we need to talk a little about the history behind it. Let's briefly look at the history of Electron.
Deep Dive Into Electron
You can also use other frameworks on top of the basic setup like Next.js, Vue.js, and Angular 5. This will make your development easier by keeping the applications more pre-defined and modularized, which in turn makes debugging and comprehension easier. You can also create unit-test cases to make sure your changes are not breaking any existing functionality.
High-Level Architecture and Use Case of Electron
I have created a simple use case diagram which shows how electron works at the core level.
Let’s see how Electron works.
The client launches the Electron application, typically from a preferred platform like Windows/MacOS/Linux-based Ubuntu.
The application makes a request to the window using the main process.
Main Process: This is responsible for sending and receiving requests and transferring data between different windows; you can think of it as a central exchange for all the windows. All memory management, like creation and destruction of windows, is handled here.
The main process makes a request to launch a window along with Renderer.js (Renderer process).
Renderer Process: This is the additional JS file we will reference in every HTML file in the window. You can access the complete DOM in the Renderer Process. You can also use it in the HTML but using the Renderer Process is a cleaner way to do that. As your application grows, you will appreciate this approach.
A sign in window will be launched and attaches to the main process, available for operations.
The user enters their login information, then you will handle the button click in the renderer process and make a request to the main process for launching the Product Window.
The main process will receive the respective information via an event listener and launch the Product window based on the request using the Renderer Process.
This loop continues for the whole application until the complete use case is solved.
Our tutorials that follow this article will discuss these concepts with a live application created using Electron, with the backend database based on Alibaba Cloud ApsaraDB for MongoDB.
Alibaba Cloud ApsaraDB for Mongo DB as the Backend
Desktop applications are prone to losing data during a system crash or any natural calamities. To save our precious data, we will be connecting our project with a backend deployed on the cloud. We will be performing some data analysis on the data stored.
The reason we choose Alibaba Cloud ApsaraDB for MongoDB because of the simple low cost and high availability. I will demonstrate my experience of how we can securely connect to this popular service and leverage it to our use.
The tutorials will be a simple two-part, step-by-step tutorial, which clarifies a lot of concepts if you are new to Electron. Part 1 of the tutorial talks about designing the front-end of the app and Part 2 of the tutorial discusses the details of the backend with ApsaraDB for MongoDB.
We will see in detail how an Electron app is created from scratch with different components like ipcRenderer, ipcMain, print functionality. We will also explore how to package/build an Electron app for production and distribution purposes. At the end of the tutorial, you should be able to create interactive charts and use the open source libraries to create an awesome UI in no time.
Whenever using a new technology, you are more likely than not to encounter some roadblocks. That's why I'll share some tips and tricks, as well as give you a heads-up for some pitfalls I faced during my development, which can save you a lot of time.
Then we will create the backend service based on Node.js using the Express node module and host it in the ECS instance. We will expose this publically for the Electron App to access.
Published at DZone with permission of Sai Sarath Chandra , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.