@signaldb/electron
Runs SignalDB's data layer in the Electron main process and connects every window to it through a MessagePort of its own. The package has an entry point for each of Electron's process contexts; the package root exports all of them.
Also check out our guide on how to use SignalDB with Electron.
createElectronHost
Import from @signaldb/electron/main
import { 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(`./data/${name}`),
})
const Posts = new Collection('posts', host.dataAdapter)Starts a WorkerDataAdapterHost in the main process and answers every window that asks for a connection with a MessageChannelMain of its own.
Options
ipcMain— Electron'sipcMain. Required.MessageChannelMain— Electron'sMessageChannelMain. Required.storage—(name: string) => StorageAdapter. Returns the storage adapter for a collection, by its name. Called once per collection, when the first connection uses it. Required.id— Must match theidofcreateElectronDataAdapter. Defaults to'signaldb-electron'; only needed when an app runs more than one host.onError— Called with errors that cannot be answered to a window as a failed request. Defaults toconsole.error.log— Receives a log line for every message the host handles.
Returns
{ dataAdapter: WorkerDataAdapter, dispose(): Promise<void> }
dataAdapter— The data adapter for collections in the main process. They work on the same data as the windows: a write on either side reaches the live queries of all others.dispose()— Stops accepting windows, disconnects every connection, and closes their ports. Dispose the collections that use the host first: a collection whose connection is gone receives no further answers.
Behavior
- A window that is closed or reloaded closes its port, and the host releases the collections and queries that window had registered.
- A collection's storage is set up when the first connection registers the collection, and released once the last one is gone.
- Every connection is a separate
MessagePort. Messages are structurally cloned; after its first answer a query only sends what changed in its result.
setupElectronPreload
Import from @signaldb/electron/preload
import { ipcRenderer } from 'electron'
import { setupElectronPreload } from '@signaldb/electron/preload'
setupElectronPreload(ipcRenderer)Relays connection requests from the window to the main process and hands the port the main process answers with to the window. A MessagePort cannot cross contextBridge, so both directions use window.postMessage. Call it once in the preload script of every window that uses createElectronDataAdapter.
Parameters
ipcRenderer— Electron'sipcRenderer.
createElectronDataAdapter
Import from @signaldb/electron/renderer
import { Collection } from '@signaldb/core'
import { createElectronDataAdapter } from '@signaldb/electron/renderer'
const dataAdapter = createElectronDataAdapter()
const Posts = new Collection('posts', dataAdapter)Creates the data adapter for a window's collections. It is a WorkerDataAdapter whose worker is the main process, so everything said there applies: queries are answered asynchronously and Cursor#isLoading() tells a query that has not been answered yet from one that matched nothing, and writes are shown optimistically until the main process confirms them.
Options
id— Must match theidofcreateElectronHost. Defaults to'signaldb-electron'.log— Receives a log line for every response, query update and failed request.
Behavior
- One adapter serves any number of collections; create one per window.
- Without
setupElectronPreloadin the window's preload script no port arrives, and the adapter fails withWorkerDataAdapter initialization timed outafter five seconds. - Disposing the last collection of the adapter closes its port, which releases the connection in the main process.
IPC channels
| Channel | Direction | Purpose |
|---|---|---|
signaldb:connect | window → main | Asks for a connection ({ id, requestId }) |
signaldb:port | main → window | Answers with the port ({ id, requestId }, port transferred) |
Everything else goes through the port.