---
url: /guides/electron.md
---

# 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:** `createElectronHost` runs 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 through `host.dataAdapter`.
* **Preload script:** `setupElectronPreload` hands each window a
  [`MessagePort`](https://www.electronjs.org/docs/latest/tutorial/message-ports)
  of its own to the main process.
* **Window:** `createElectronDataAdapter` creates 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`](/reference/core/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](https://www.electronjs.org/docs/latest/tutorial/quick-start)
to set one up.

## Installation

Install SignalDB core and the Electron integration:

```bash
  npm install @signaldb/core @signaldb/electron
```

You also need a storage adapter for the main process. For example, to store the
data in the filesystem:

```bash
  npm install @signaldb/fs
```

And a reactivity adapter for the window. For example, if you are using React:

```bash
  npm install @maverick-js/signals @signaldb/maverickjs @signaldb/react
```

## Setting 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.

```js
// 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`:

```js
// 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`.

```js
// 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()`](/reference/core/cursor/) 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](/reference/fs/), or [build your own](/reference/core/createstorageadapter/)
* Add [Synchronization](/sync/) to keep data in sync with a backend
* See the full [API Reference](/reference/electron/) for `@signaldb/electron`
