Skip to main content

Integrating with Electron

This guide will walk you through the process of setting up and utilizing the SDK within your project.

info

Don't forget to generate your Honeygain SDK API Key and download SDK from Developers dashboard first.

Installation

First, ensure you have Node.js and npm installed on your development environment. Then, use npm to install the Honeygain Electron SDK from the package tarball:

npm install ./honeygain-electron-sdk.tgz

Integration Steps

1. Create SDK Instance

Initialize the Honeygain Electron SDK by creating a new instance with your api key:

import { HgSdk } from "@honeygain/electron-sdk";

const sdk = new HgSdk({
apiKey: "<your api key here>",
});

2. Bind Methods in Main Process

Within your Electron main process (typically in your main.js or main.ts file), bind methods to handle SDK operations and events using Electron's ipcMain event emitter:

import { ipcMain } from "electron";

ipcMain.handle("hg-sdk:start", () => sdk.start());
ipcMain.handle("hg-sdk:stop", () => sdk.stop());
ipcMain.handle("hg-sdk:request-consent", () => sdk.requestConsent());
ipcMain.handle("hg-sdk:is-running", () => sdk.isRunning);
ipcMain.handle("hg-sdk:is-opted-in", () => sdk.isOptedIn);
ipcMain.handle("hg-sdk:opt-in", () => sdk.optIn());
ipcMain.handle("hg-sdk:opt-out", () => sdk.optOut());

sdk.on("is-running", (isRunning) => {
mainWindow.webContents.send("hg-sdk:is-running", isRunning);
});

sdk.on("is-opted-in", (isOptedIn) => {
mainWindow.webContents.send("hg-sdk:is-opted-in", isOptedIn);
});

3. Bind Methods in Preload Script (Renderer Process)

In the preload script of your Electron renderer process (typically a script linked in your Electron BrowserWindow settings), expose SDK functionality to the frontend using Electron's contextBridge:

import { contextBridge, ipcRenderer } from "electron";

const hgSdk = {
start: () => ipcRenderer.invoke("hg-sdk:start"),
stop: () => ipcRenderer.invoke("hg-sdk:stop"),
requestConsent: () => ipcRenderer.invoke("hg-sdk:request-consent"),
isRunning: () => ipcRenderer.invoke("hg-sdk:is-running"),
isOptedIn: () => ipcRenderer.invoke("hg-sdk:is-opted-in"),
optIn: () => ipcRenderer.invoke("hg-sdk:opt-in"),
optOut: () => ipcRenderer.invoke("hg-sdk:opt-out"),

changeConsent: (consent: boolean) =>
ipcRenderer.send("hg-sdk:change-consent", consent),
onIsRunningChange: (callback: (status: boolean) => void) => {
ipcRenderer.on("hg-sdk:is-running", (_event, isRunning) => {
callback(isRunning);
});
},
onIsOptedInChange: (callback: (status: boolean) => void) => {
ipcRenderer.on("hg-sdk:is-opted-in", (_event, isOptedIn) => {
callback(isOptedIn);
});
},
};

contextBridge.exposeInMainWorld("hgSdk", hgSdk);

4. Usage in Renderer Process (Frontend)

Now you can use the exposed hgSdk object in your renderer process to interact with the Honeygain Electron SDK:

window.hgSdk.start(); // Start the SDK
window.hgSdk.stop(); // Stop the SDK
window.hgSdk.isRunning(); // Check if the SDK is running
window.hgSdk.isOptedIn(); // Check if consent is given
window.hgSdk.optIn(); // Give consent
window.hgSdk.optOut(); // Revoke consent
window.hgSdk.requestConsent(); // Open the consent window if needed

window.hgSdk.onIsRunningChange((status) => {
console.log("SDK status changed:", status);
}); // Listen for SDK status changes
window.hgSdk.onIsOptedInChange((status) => {
console.log("Opt in changed:", status);
}); // Listen for consent changes

Logging

To enable logging to console by default, you can set the enableLogging option to true when creating the SDK instance:

const sdk = new HgSdk({
apiKey: 'your-api-key-here',
enableLogging: true,
});

This will log all SDK events to the console, including session start, stop, and heartbeat events. You can also customize the logging by providing a custom logger function that takes a message as an argument. This allows you to log messages to a file or any other logging service of your choice.

const sdk = new HgSdk({
apiKey: 'your-api-key-here',
enableLogging: true,
loggerCallback: (message) => {
// Custom logging logic
// For example, log to a file or send to a logging service
console.log(message);
}
});

Example log messages include:

info: Session started <session-id>
info: Heartbeat received
info: Heartbeat sent
info: Session stopped

error: Unable to resolve host
error: Unable to connect to server

Conclusion

By following these steps, you'll successfully integrate and utilize the Honeygain Electron SDK in your Electron application. If you have any questions or encounter issues during integration, please reach out to us at [email protected] for support. Happy coding!