Desktop App Architecture (Electron)
Relevant source files
The following files were used as context for generating this wiki page:
The Ever Teams Desktop App (server-web) is an Electron-based application that serves as a desktop wrapper for the main Ever Teams Next.js web application. It provides a native-like experience by integrating with the host operating system for features like a system tray, native menus, and auto-updates, while also managing a local instance of the web server.
This architecture is built upon the Electron React Boilerplate (ERB), providing a solid foundation with established patterns for development, building, and packaging. The primary goal is to deliver the web application's full functionality within a dedicated desktop client.
Sources: apps/server-web/README.md, README.md:155
Core Architecture
The application follows the standard Electron multi-process architecture, comprising a Main process, multiple Renderer processes, and a Preload script to bridge them. A key addition is the management of a separate Node.js process for the web server backend.
This diagram illustrates the high-level components and their interactions.
Sources: apps/server-web/src/main/main.ts
Main Process
The Main process, with its entry point at apps/server-web/src/main/main.ts, is the backbone of the application. It has full Node.js access and is responsible for all backend operations and native OS integration.
Key responsibilities include:
- Application Lifecycle Management: Handling events like
app.whenReady(),window-all-closed, andbefore-quit. - Window Management: Creating and managing all
BrowserWindowinstances using aWindowFactory. - Backend Server Control: Starting, stopping, and restarting the Next.js web server via the
DesktopServerclass. - Inter-Process Communication (IPC): Listening for and responding to messages from Renderer processes via
ipcMain. - System Tray: Initializing and updating the application's system tray icon and context menu.
- Auto-Updates: Managing the application update process using
electron-updaterthrough a dedicatedUpdaterclass. - Native Menus: Building and setting the application's main menu and context menus using
MenuBuilder. - Configuration: Reading and writing application settings using
electron-storevia theLocalStorehelper.
Sources: apps/server-web/src/main/main.ts:25-28, 301-314, 401-419, 574-633
Renderer Process
Each window in the application runs its own Renderer process. These are essentially web pages running in a Chromium browser environment. The UI is built with React.
- Entry Point: The main React application is loaded via
apps/server-web/src/renderer/App.tsx. - Routing:
react-router-domis used to manage navigation between different pages within a window (e.g.,AboutPage). - UI Components: The UI is composed of React components, some of which are specific to the desktop app, like
WindowControl. - Communication: Renderer processes cannot directly access Node.js APIs. They communicate with the Main process using the
ipcRendererAPI exposed by the preload script.
Sources: apps/server-web/src/renderer/App.tsx, apps/server-web/src/renderer/pages/About.tsx
Preload Script
The preload script, apps/server-web/src/main/preload.ts, acts as a secure bridge between the Renderer and Main processes. It runs in a privileged context with access to both the window object of the renderer and Node.js APIs.
It uses Electron's contextBridge to expose a controlled set of APIs to the renderer under the window.electron global object. This prevents the renderer from having unrestricted access to powerful Node.js modules, which is a critical security practice.
// apps/server-web/src/main/preload.ts:10-29
const electronHandler = {
ipcRenderer: {
sendMessage(channel: IpcChannel, args: unknown) {
ipcRenderer.send(channel, args);
},
on(channel: IpcChannel, func: (...args: unknown[]) => void) {
// ...
},
once(channel: IpcChannel, func: (...args: unknown[]) => void) {
// ...
},
invoke(channel: string, ...args: any[]) {
return ipcRenderer.invoke(channel, ...args);
},
// ...
},
};
contextBridge.exposeInMainWorld('electron', electronHandler);
Sources: apps/server-web/src/main/preload.ts:10-33
Application Startup and Lifecycle
The application's startup sequence is orchestrated in main.ts. It determines whether to show the initial setup window or the main server log window based on stored configuration.
This diagram shows the application startup flow.
A critical lifecycle event is before-quit, which ensures the backend server is stopped gracefully before the application exits, prompting the user for confirmation if the server is running.
Sources: apps/server-web/src/main/main.ts:705-723
Inter-Process Communication (IPC)
IPC is fundamental to the app's operation, allowing the UI (Renderer) to trigger actions in the backend (Main). The system uses a predefined set of channels and message types for structured communication.
Key IPC Channels
Channel (IPC_TYPES) | Direction | Description |
|---|---|---|
SETTING_PAGE | Main <=> Renderer | Handles all communication related to settings, including saving, loading, language changes, and update status. |
SERVER_PAGE | Main <=> Renderer | Manages communication about the backend server's status (running/stopped) and execution commands. |
CONTROL_BUTTON | Renderer -> Main | Used by custom title bar buttons in the renderer to trigger native window actions like close and minimize. |
current-theme | Renderer -> Main (invoke) | Fetches the current theme setting from the Main process. |
get-platform | Renderer -> Main (invoke) | Fetches the host operating system platform (win32, darwin, etc.). |
Sources: apps/server-web/src/main/main.ts:635-690, 692-703, apps/server-web/src/renderer/pages/About.tsx:32-39
Example IPC Flow: Checking for Updates
The following sequence diagram illustrates how the Settings window initiates an update check.
Window Management
The WindowFactory class is responsible for creating all BrowserWindow instances. This centralizes window configuration, such as dimensions, web preferences (including the preload script path), and assigning the native menu.
The application defines several distinct window types:
SETUP_WINDOW: For initial server configuration.LOG_WINDOW: The main window, showing server logs and status.SETTING_WINDOW: For managing application and server settings.ABOUT_WINDOW: Displays application information.
Custom window controls (close, minimize) are implemented using custom-electron-titlebar. The renderer sends an IPC message on the CONTROL_BUTTON channel, and the main process calls the corresponding BrowserWindow method.
Sources: apps/server-web/src/main/main.ts:101-104, 131-149, 301-304
Backend Server Integration
The DesktopServer class in main.ts manages the lifecycle of the Next.js web application server.
- Starting: The
runServerfunction executes the compiled Next.js server file (standalone/apps/web/server.js) as a child process. It passes necessary environment variables, such asIS_DESKTOP_APPandNEXT_SHARP_PATH. - Events: The
DesktopServeruses anEventEmitterto signal when the server has started (webServerStarted) or stopped (webServerStopped). The Main process listens for these events to update the UI and tray menu accordingly. - Stopping: The
stopServerfunction gracefully terminates the server's child process.
Sources: apps/server-web/src/main/main.ts:316-343, 421-444
Development and Build
The apps/server-web/package.json file contains all the scripts and dependencies for developing and building the desktop application.
Key Scripts
| Script | Description |
|---|---|
start | Starts the renderer development server. |
start:main | Starts the main process in development mode using electronmon for hot-reloading. |
build | Builds the main and renderer processes for production. |
package | Packages the application into a distributable format (e.g., .dmg, .exe) using electron-builder without publishing. |
pack:mac, pack:win, pack:linux | Builds and packages the application for a specific OS and publishes it. |
Sources: apps/server-web/package.json:19-35
Build Configuration
The build section in package.json configures electron-builder. It defines:
appId:com.ever.everteamswebserver.files: Specifies which files to include in the final package.extraResources: Includes assets like the standalone web server build (standalone/**) and locales.- OS-specific settings: Defines targets (
dmg,zip,AppImage,nsis), icons, and code-signing configurations for macOS, Windows, and Linux. - Publishing: Configured to publish releases to the
ever-co/ever-teamsGitHub repository.
Sources: apps/server-web/package.json:115-188
For development, electronmon is used to automatically restart the main process when its source files change, improving the developer workflow.
Sources: apps/server-web/package.json:32, [apps/server-web/electronmon.js]