Repository navigation
Description
I have a feeling the answer to my question is "no", but I'm going to ask anyways in hopes that this might be considered for a feature (if I can't already do it).
Per docs on Application Distribution, we are advised to "name the folder of your app as app, and put it under Electron's resources directory" (e.g. Electron.app/Contents/Resources/app/ or app.asar).
I suppose that works for 99% of use cases -- and it works great. However I am beginning to explore the idea of using Electron as a container/shell for distributing multiple "apps", with the general idea being that our users would download our company-branded Electron container (i.e. Electron.app) but be able to choose from a list of "apps" (i.e. app1.asar, app2.asar, etc).
Is that possible? Or if not, can it be?
Why would I even want to do this?
If my company has multiple applications to distribute to users, we don't necessarily want to ask them to download multiple things -- plus it wastes space, and keeping users on the latest version of each app becomes a nightmare. Having multiple apps, accessible from the same Electron runtime as individual child processes seems like the ideal solution.
Can't you just ship your "apps" inside the same "app.asar"?
Technically, yes we could. But again, long-term maintenance becomes a nightmare. If each "app" has its own delivery schedule, development team, etc then clearly having separate folders/asars has its advantages. Furthermore, the concept of auto-updating individual apps becomes far easier.
Can anyone chime in with thoughts or comments about attempting something like this? I get the feeling that the /app/ (and /app.asar) folder is hard-coded as the only way to do things.
Activity
anaisbetts commentedon Aug 4, 2015
Waste the space, disk space is cheap.
arthurakay commentedon Aug 4, 2015
Assuming my users have infinite disk space on their machines, it still leaves me with the rather unsavory option of asking them to install multiple binaries. The long-term struggle to keep the users on the latest versions of those binaries is also unpalatable.
Based on my experience with Electron thus far, and your terse response, I take it that what I'm asking for currently isn't possible.
So let's tackle the more interesting question here -- can it (or should it) be something Electron supports?
Electron clearly hasn't been designed with this use case in mind, but it strikes me that the ability to launch multiple apps from the same runtime could be done.
etiktin commentedon Aug 4, 2015
Yes. It is possible (at least if I understand you correctly). The default_app supports loading other apps dynamically. Take a look at main.js (specifically follow the handling of
option.file).Your default asar could show the list of apps and when the user choose one, you will load it like the default_app does (
require('module')._load(packagePath, module, true);).Another option is to create a shell script for each app, that will call your branded electron and pass to it as an argument the path to the app you want to load (which is exactly what the default_app does).
arthurakay commentedon Aug 4, 2015
@etiktin That is incredibly helpful, I didn't realize some of this infrastructure already existed! I guess I have three follow-up questions at this point:
(1)
app.setAppPath(packagePath);implies that Electron can only ever have one of these sub-apps open at a time. Is that accurate? Users should expect that they can open any of the apps in the list simultaneously, so I just want to be sure that scenario "just works" with this approach. (Each app would launch in its ownBrowserWindow).(2) The code I see in
main.jsuses apackagePathto a folder containing the "app" assets. If I were attempting to distribute my "apps" using the.asararchive format, is that just magically readable at runtime?(3) In the following code from
main.js:Where is the
modulemodule defined? I'm just generally curious how that works, where the second parameter "module" comes from, and what else this might do.Thanks again for the idea!
etiktin commentedon Aug 4, 2015
Glad I could help 😄
If it's ok to close, just load the app like the default_app does. Then when the user wants to select another app, he'll re-run your app which will open it in a new electron instance (you can run more than one electron process) and he/she can select another app.
If you want the list to stay open, I would suggest that you support accepting app path as an argument (like the default_app does). If you didn't get an app path, it means you need to show the list, otherwise load the app in that path. So when a user clicks on an app you would spawn a new electron instance with the app path as an argument (you can do it using the
child_processmodule).asarpackages are supposed to work like app directories (there's some magic involved in making it work like that). So just run electron with the default_app and either pass to it as an argument the path to a testasaror just drag and drop it over and see if it works. If it doesn't, open a new issue for that and it's probably something that can be worked around.moduleis a node/io.js builtin module that implements the module system.arthurakay commentedon Aug 4, 2015
Awesome, thanks again! I've got a lot of work ahead of me, but this answers a lot of questions.
bsacks99 commentedon Nov 23, 2016
@arthurakay,
Were you able to come up with a solution that works for having multiple applications? I have a similar issue and am trying to find a starting point. @etiktin shell script idea might be the best suited for us. Wondering where you landed in the end.
Thanks,
B
dovk commentedon Dec 14, 2019
I hope this is clear enough and might help someone - if you need more details, please write to me. I did this in a large organization with thousands of Windows workstations. I assume adjustments need to be made for other platforms, but here is the general idea -
I have one initial Electron app (main only) -"root" - whose job is to run "child" Electron apps. In my case these are internal corporate apps that are not initiated by the user but rather run automatically all the time - so my solution was to have the "root" app run automatically every few minutes from the Windows Task Scheduler. If it is already active, the scheduler will not call it. When active, the "root" app sends the workstation user name to my server (NodeJS/Express using mySQL), which sends back an array of apps relevant to this user. It then calls each child app in the array. If a new app is added later on, there is no change to the "root" app - the new app will appear as another app in the array coming from the server.
The
mainController()calls a recursive tail functioncallApps()with an index starting from zero, the apps array and its length (number of apps)callApps()runs through the array and calls a function calledcall()with the name of the app to call.The "root" app will also pass the "root" app pid (from process.pid) to each child app. I will expand on that later.
To call the app as a child process we need to define above
const child = require('child_process').execFile
This is the call() function -
The final function
exe()runs a .bat file calledrun.bat. I pass it the child app name, which is also the name of the folder where the child app resides. In the case of a new app - the "root" app will also create the new app folder.run.batthen callselectron.exe-node_modules\electron\dist\electron.exe app\%1\main.js %1 --disable-gpu --ignore-gpu-blacklist --disable-gpu-compositingThis is the exe() function -
Since I am running multiple Electron apps, the single instance mechanism cannot be enforced, so I did the following to ensure that only one instance of the "root" app and each child app will run at a given time -
(1) When a child app starts to run it retrieves it's current pid via process.pid. It will then check to see if there is no other instance of this pid running, and if not, it will write the pid to a file in it's own app folder called pid.txt.
To check if there is another active instance of the app, it first reads the pid.txt file and checks to see if that pid exists on the system tasklist as follows:
If the child app's pid is in the tasklist, the child app will shut itself down immediately via closeApp(). If not, it will write it's current pid to the file and continue.
(2) The same is done by the "root" app - it checks that it's pid is not on the system tasklist, and then proceeds to call the child apps. Before calling each child app it will check that the child app does not have a running instance by reading the pid.txt file from the relevant child app folder and searching the system tasklist for that pid.
(3) As mentioned above, the "root" app passes it's pid to each child app. After each child app completes it's single instance check, it then checks the tasklist to see if the "root" app pid is active.
If the "root" app is not active, each child app will shut itself down, so that the end result will be that if the "root" app is not active, no child app will be active either.
The "root" is activated automatically every few minutes via the Task Scheduler, and as soon as it is active, it will then activate all the child apps.
I assume there is a more elegant way to achieve this, but this works well.
One last point - since it is not practical to reinstall the app to thousands of workstations upon every code change, I placed all the code files in a database table on the server. The "root" app checks for code changes every few minutes, and when there are code changes, it retrieves the code from the server and updates the relevant files in the relevant folders on each client. After doing so the "root" app shuts itself down, and as result, all child apps do so as well. This is OK since the "root" app will again be activated within minutes by the Task Scheduler, it will then activate the child apps, and the new code will become effective.
The code sent by the server is obfuscated (using Javascript Obfuscator).
YassineElHosni commentedon Jul 5, 2021
Any chance you can share your code (a repository maybe), I am out of my comfort zone, and what you are suggesting will probably take me days to elaborate. Please help.
dovk commentedon Jul 5, 2021
YassineElHosni commentedon Jul 5, 2021
@dovk thanks in advance
dovk commentedon Jul 6, 2021
I'm not working on it now, it might have changed a bit since the previous post, but the idea is still the same
Take a look here
and if it's not clear, let me know and we can go over it.
YassineElHosni commentedon Jul 6, 2021
Thanks a lot @dovk, much appreciated.