Use SignalDB with Electron
In this guide, you will learn how to use SignalDB in an Electron application. Electron separates the main process from the windows (renderer processes), and storage such as the filesystem is only reachable from the main process. With @signaldb/electron the main process runs SignalDB's data layer on a storage adapter of your choice, and the main process as well as every window work with ordinary, reactive collections on that data.
How it works
- Main process:
createElectronHostruns the data layer. It owns the storage adapters, executes queries and writes, and keeps the live queries up to date. Collections in the main process use it throughhost.dataAdapter. - Preload script:
setupElectronPreloadhands each window aMessagePortof its own to the main process. - Window:
createElectronDataAdaptercreates the data adapter for the window's collections. Queries and writes go to the main process through the port.
A write from a window or from the main process reaches the live queries everywhere else. Writes are shown optimistically in the window that made them, and after the first answer only what changed in a query's result crosses the process boundary. Under the hood this is the WorkerDataAdapter with the main process in the role of the worker.
Prerequisites
We assume you already have a basic Electron project with a main process, a preload script, and a renderer process. If you don't, follow the Electron Quick Start to set one up.
Installation
Install SignalDB core and the Electron integration:
npm install @signaldb/core @signaldb/electronYou also need a storage adapter for the main process. For example, to store the data in the filesystem:
npm install @signaldb/fsAnd a reactivity adapter for the window. For example, if you are using React:
npm install @maverick-js/signals @signaldb/maverickjs @signaldb/reactSetting Up the Main Process
Call createElectronHost once, before the first window is created. storage returns the storage adapter for a collection, by the collection's name.
// main.js
import path from 'node:path'
import { app, BrowserWindow, ipcMain, MessageChannelMain } from 'electron'
import { Collection } from '@signaldb/core'
import { createElectronHost } from '@signaldb/electron/main'
import createFilesystemAdapter from '@signaldb/fs'
const host = createElectronHost({
ipcMain,
MessageChannelMain,
storage: name => createFilesystemAdapter(path.join(app.getPath('userData'), name)),
})
// Collections in the main process share their data with every window
const Posts = new Collection('posts', host.dataAdapter)
ipcMain.handle('get-post-count', () => Posts.find({}, { async: true }).count())
app.whenReady().then(() => {
const win = new BrowserWindow({
webPreferences: {
preload: path.join(import.meta.dirname, 'preload.js'),
},
})
win.loadFile('index.html')
})Outside of a reactive scope, read with { async: true } as everywhere else in SignalDB.
Setting Up the Preload Script
In the preload script of every window that uses SignalDB, call setupElectronPreload:
// preload.js
import { ipcRenderer } from 'electron'
import { setupElectronPreload } from '@signaldb/electron/preload'
setupElectronPreload(ipcRenderer)A MessagePort cannot cross contextBridge, so the preload script relays the port with window.postMessage instead — the way the Electron documentation recommends. This works with and without context isolation. The window gets no access to ipcRenderer itself.
Using Collections in the Window
Create one data adapter per window with createElectronDataAdapter and pass it to your collections. Collection names are the names the main process passes to storage.
// renderer.js
import { Collection } from '@signaldb/core'
import maverickReactivityAdapter from '@signaldb/maverickjs'
import { createElectronDataAdapter } from '@signaldb/electron/renderer'
const dataAdapter = createElectronDataAdapter()
export const Posts = new Collection('posts', dataAdapter, {
reactivity: maverickReactivityAdapter,
})
export const Users = new Collection('users', dataAdapter, {
reactivity: maverickReactivityAdapter,
})From here, use the collections like any other SignalDB collection. Queries are answered asynchronously: until the main process has answered, a query returns its neutral empty result, and Cursor#isLoading() tells that apart from a query that matched nothing.
If the preload script does not call setupElectronPreload, no port arrives and the adapter fails with WorkerDataAdapter initialization timed out after five seconds.
Multiple Windows
Every window gets a connection of its own. When a window is closed or reloaded, its port closes and the main process releases everything that window had registered. A collection's storage is set up once, when the first connection uses it, no matter how many windows use it.
Conclusion
The main process owns the data and works with it through ordinary collections, the preload script hands each window a port, and every window's collections stay in sync with the main process and with each other.
Next Steps
- Choose a storage adapter for the main process: Filesystem, or build your own
- Add Synchronization to keep data in sync with a backend
- See the full API Reference for
@signaldb/electron