Skip to content

Multiple "apps" #2415

Closed
Closed

Description

@arthurakay

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

  1. anaisbetts commented on Aug 4, 2015

    @anaisbetts
    Contributor

    Waste the space, disk space is cheap.

  2. arthurakay commented on Aug 4, 2015

    @arthurakay
    Author

    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.

  3. etiktin commented on Aug 4, 2015

    @etiktin
    Contributor

    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).

  4. arthurakay commented on Aug 4, 2015

    @arthurakay
    Author

    @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 own BrowserWindow).

    (2) The code I see in main.js uses a packagePath to a folder containing the "app" assets. If I were attempting to distribute my "apps" using the .asar archive format, is that just magically readable at runtime?

    (3) In the following code from main.js:

    // Run the app.
    require('module')._load(packagePath, module, true);
    

    Where is the module module 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!

  5. etiktin commented on Aug 4, 2015

    @etiktin
    Contributor

    Glad I could help 😄

    1. After the user selected the app do you want the menu (app list) to stay open or is it ok to close it?
      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_process module).
    2. asar packages 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 test asar or 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.
    3. module is a node/io.js builtin module that implements the module system.
  6. arthurakay commented on Aug 4, 2015

    @arthurakay
    Author

    Awesome, thanks again! I've got a lot of work ahead of me, but this answers a lot of questions.

  7. bsacks99 commented on Nov 23, 2016

    @bsacks99

    @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

  8. dovk commented on Dec 14, 2019

    @dovk

    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 function callApps() with an index starting from zero, the apps array and its length (number of apps)

    function mainController() {
        let arr = userAppData
        let len = arr.length
        callApps(0, len, arr).then(() => {
        })
    }
    

    callApps() runs through the array and calls a function called call() 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.

    function callApps(i, len, arr) {
        return new Promise(resolve => {
            if (i >= len) {
                resolve()
            } else {
                    let appId = arr[i].appId
                    call(appId).then(() => { // call the app
                        let j = i + 1
                        resolve(callApps(j, len, arr)) // recursive tail loop
                    })
            }
        })
    }
    

    To call the app as a child process we need to define above
    const child = require('child_process').execFile
    This is the call() function -

    function call(appId) {
        return new Promise((resolve) => {
            let cmd = cwd + '\\run.bat'
            let parm = []
            parm.push(appId)
            exe(cmd, parm).then(() => {
                resolve()
            })
        })
    }
    

    The final function exe() runs a .bat file called run.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.bat then calls electron.exe -
    node_modules\electron\dist\electron.exe app\%1\main.js %1 --disable-gpu --ignore-gpu-blacklist --disable-gpu-compositing
    This is the exe() function -

    function exe(cmd, parm) {
        return new Promise((resolve) => {
            child(cmd, parm, function (err, data) {
                if (err) {
                    console.log(err)
                    resolve()
                } else {
                    resolve()
                }
            })
            resolve()
        })
    }
    

    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:

    exec('tasklist', function (err, stdout, stderr) { 
    // search in the stdout string
     }
    

    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).

  9. YassineElHosni commented on Jul 5, 2021

    @YassineElHosni

    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 function callApps() with an index starting from zero, the apps array and its length (number of apps)

    function mainController() {
        let arr = userAppData
        let len = arr.length
        callApps(0, len, arr).then(() => {
        })
    }
    

    callApps() runs through the array and calls a function called call() 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.

    function callApps(i, len, arr) {
        return new Promise(resolve => {
            if (i >= len) {
                resolve()
            } else {
                    let appId = arr[i].appId
                    call(appId).then(() => { // call the app
                        let j = i + 1
                        resolve(callApps(j, len, arr)) // recursive tail loop
                    })
            }
        })
    }
    

    To call the app as a child process we need to define above
    const child = require('child_process').execFile
    This is the call() function -

    function call(appId) {
        return new Promise((resolve) => {
            let cmd = cwd + '\\run.bat'
            let parm = []
            parm.push(appId)
            exe(cmd, parm).then(() => {
                resolve()
            })
        })
    }
    

    The final function exe() runs a .bat file called run.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.bat then calls electron.exe -
    node_modules\electron\dist\electron.exe app\%1\main.js %1 --disable-gpu --ignore-gpu-blacklist --disable-gpu-compositing
    This is the exe() function -

    function exe(cmd, parm) {
        return new Promise((resolve) => {
            child(cmd, parm, function (err, data) {
                if (err) {
                    console.log(err)
                    resolve()
                } else {
                    resolve()
                }
            })
            resolve()
        })
    }
    

    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:

    exec('tasklist', function (err, stdout, stderr) { 
    // search in the stdout string
     }
    

    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).

    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.

  10. dovk commented on Jul 5, 2021

    @dovk
  11. YassineElHosni commented on Jul 5, 2021

    @YassineElHosni

    @dovk thanks in advance

  12. dovk commented on Jul 6, 2021

    @dovk

    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.

  13. YassineElHosni commented on Jul 6, 2021

    @YassineElHosni

    Thanks a lot @dovk, much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions