electron-data-exchanger
v1.0.1
Published
Data Exchanger for Electron
Downloads
1
Readme
Exchange data between the Main Process and Renderers (EXPERIMENTAL).
Installation
npm install electron-data-exchanger
The code below goes into your "index.html". It is only necessary if you want to avoid having passing the full path to the library during the ESM import.
<script type="importmap">
{
"imports": {
"electron-data-exchanger": "../node_modules/electron-data-exchanger/esm/index.mjs"
}
}
</script>
Usage
MAIN PROCESS
// We use the CommonJs format in main
const MainDataExchanger = require("electron-data-exchanger").MainDataExchanger;
const dataExchanger = new MainDataExchanger()
await dataExchanger.init(mainWindow)
// Monitor RENDERER messages
dataExchanger.addListener(UserChannelIdentifier, function (data)
{
return {MainResponse: "Main___Response", youSent: data}
})
// Send message to the Renderer
await dataExchanger.sendMessage("my-channel", {hi: "Message from MAIN"})
PRELOAD
const proxyExchanger = require("electron-data-exchanger").ProxyExchanger;
const dataExchanger = new ProxyExchanger();
// We must pass all the channels
dataExchanger.enableProxy(["my-channel"])
RENDERER
// We use the ESM format in the view
import {DomDataExchanger} from "electron-data-exchanger";
// Initialise Data Exchangers
const dataExchanger = new DomDataExchanger()
await dataExchanger.init()
// Monitor MAIN PROCESS messages
await dataExchanger.addListener(UserChannelIdentifier, function (data)
{
return {RendererResponse: "***", originalCaller: JSON.stringify(data)};
});
// Send a message to main
const response = await dataExchanger.sendMessage("my-channel", {hello: "Message from RENDERER"}, true);