Packaging/Debugging Electron Apps for roElectron
Overview
Electron applications consist of two main elements: the Electron framework and the user's application code. On traditional desktop environments, the Electron framework is bundled with the application, either through an installer that deploys both the Electron framework and the application, or as a standalone application that encapsulates and runs the user application.
The BrightSign platform employs a modified versions of the libraries used by Electron, leading to compatibility issues with standard Electron builds due to differences in libc++ and restrictions on executing binaries from SD cards or other user-accessible storage. Consequently, traditional Electron deployment methods like those generated by electron-forge are not viable on BrightSign. Additionally, BrightSign's modifications to Electron are not contributed back to the Electron project.
To circumvent these issues, BrightSign utilizes ASAR archives, which Electron can natively understand, in combination with a custom Electron build provided by the BrightSign Operating System (BOS). The roElectron widget serves as the bridge for launching ASAR-packaged applications with this customized Electron framework.
Packaging Apps
The Electron application needs to be packaged for Linux ARM64 before it can be used with roElectron. You can use tools like electron-builder, electron-packager, or electron-forge to create the Linux ARM64 package. Make sure to specify the correct architecture and platform during the packaging process. We do not need the Electron binary itself when packaging an application for use with roElectron, as the BrightSign player provides the necessary runtime.
For example, if you have an Electron application in a folder called my-electron-app after packaging it for Linux ARM64, you will have a folder structure similar to the following:
You need to copy the content of the resources folder to the BrightSign player. The apps main url may then be something like file://sd://resources/app/dist/main.js. If you package your app as an ASAR, this will make it easier to manage and distribute as a single file, and in this case the main url would be something like file://sd://resources/app/dist/app.asar.
Enabling ASAR packaging simplifies the deployment process and ensures that all application files are bundled together, reducing the risk of missing files on the BrightSign player. Enabling it is simple when using tools like electron-builder or electron-forge, which provide options to package the application as an ASAR archive. For example to enable ASAR when using electron-forge, you can add the following to your forge.config.js:
module.exports = {
packagerConfig: {
asar: true
}
};If your app has native binaries, you need to add to forge.config.js:
module.exports = {
plugins: [
// Moves .node binaries to app.asar.unpacked so they can be dlopen'd.
{name: '@electron-forge/plugin-auto-unpack-natives', config: {}},
],
};Running the Application
Copy the packaged application to the BrightSign player SD card or SSD and run it from there. The SD card and SSD are mounted with execute permissions, so applications that include native binaries can also be run directly from these locations.
For example if the packaged application has been copied to the SD card in the root directory in a folder called my-electron-app, you can run it using the following command:
electron = CreateObject("roElectron", "file://sd://my-electron-app/main.js", {
app_name: "MyApp",
message_port: msgPort
})Automatically running an app.asar
If you package your application as an ASAR archive, you can run it without writing any BrightScript. Copy app.asar (and app.asar.unpacked, if your app has native binaries) from the resources folder to the root of a storage drive, for example the SD card. app.asar is used because it is the name Electron looks for by default, and the name that Electron Forge and @electron/packager always give the archive. app.asar is only picked up as the last step of the normal startup process, when there are no other autorun files on the drive. An existing autorun.brs, autorun.bas or autorun.zip always takes priority. Things to be aware of:
- If the Electron app exits, the player will not automatically restart it or reboot.
- The app_name is always app-autorun, so every automatically run app uses the same user data directory, <root>/.home/.config/app-autorun. If you run different Electron apps this way at different times from the same drive, they share their data, for example their settings, cookies and local storage. Delete that directory when you switch apps if they must not share data, or write your own autorun.brs with a unique app_name.
App Storage
Electron provides a standard way to get the storage paths for your application using the app.getPath(name) method in the main process. Common storage locations include:
- appData: Per-user application data directory.
- userData: The directory for storing your app's configuration files.
- temp: Temporary files directory.
- desktop: The user's desktop directory.
- documents: The user's documents directory.
- downloads: The user's downloads directory.
- music: The user's music directory.
- pictures: The user's pictures directory.
- videos: The user's videos directory.
Apart from temp, these paths are derived from either the user data or home directory. roElectron places both on the drive that the application is launched from:
- Home directory: (HOME): <root>/.home
- User data directory: (--user-data-dir): <root>/.home/.config/<app_name>
where <root> is the storage drive (for example, sd:/ for file://sd:/app.asar). These directories are created automatically if they do not exist. Launching an application from anywhere else, for example an http:// URL, is an error.
Known issues
- electron-vite source code protection needs to be disabled when using roElectron. In order to use this feature the application needs to be built using the exact Electron binary. However, this is not possible when using the BrightSign Electron runtime.
- Apps that use native binaries must include ARM64 binaries as well, to ensure compatibility with BrightSign players that use ARM64 architecture.
- Browser window (setAlwaysOnTop) is not supported on Linux devices and may also not work as expected when using roElectron as the z-order is managed by the BrightSign compositor rather than the Electron runtime.
- (Desktop capturer) is not supported when using roElectron as it relies on Electron's ability to capture the desktop, which is not available in the BrightSign environment.
- Spawning a new Electron instance from an existing Electron instance using child_process is not supported when using roElectron.
Debugging Electron
To enable remote debugging of the Electron application main process on a BrightSign player, you can pass the --inspect argument via the arguments parameter when creating the roElectron object. For example:
electron = CreateObject("roElectron", "file://sd://main.js", {
app_name: "MyApp",
message_port: msgPort,
arguments: ["--inspect=0.0.0.0:9229"]
})Note: If using ipv6, replace 0.0.0.0 with :: in the --inspect argument.
You can then use Chrome DevTools or VSCode to connect to the Electron application's remote debugging port.
Debugging from VSCode
To debug the Electron application main process from VSCode, you need to configure a launch configuration in your .vscode/launch.json file. Here is an example configuration:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach to Electron",
"port": 9229,
"address": "<IP Address of BrightSign Player>",
"localRoot": "${workspaceFolder}",
"remoteRoot": "/storage/sd/main.js"
}
]
}Then start the Electron application with the --inspect-brk argument, and VSCode will be able to attach to it for remote debugging. For example:
electron = CreateObject("roElectron", "file://sd://main.js", {
app_name: "MyApp",
message_port: msgPort,
arguments: ["--inspect-brk=0.0.0.0:9229"]
})Note: If using ipv6, replace 0.0.0.0 with :: in the --inspect-brk argument.
Debugging the Electron application renderer process
This can be done as normal for the Electron app running on a desktop environment, using the standard Electron developer tools for the renderer process.