IPC
A deep dive into Gluon's IPC.
On this page
IPC (Inter-Process Communication) is how your Node backend and Web frontend communicate with each other. There are 3 “sub-APIs” in IPC you can use:
Expose Functions
You can easily directly “expose” Node functions to the Web frontend, with a simple wrapper around IPC events built-in. Exposed functions are always async (due to using IPC internally).
// In your Node backend
import * as Gluon from '@gluon-framework/gluon';
const Window = await Gluon.open('https://gluonjs.org');
// Expose our own function to write messages to a log file
import { writeFile } from 'fs/promises';
let log = '';
Window.ipc.log = async msg => {
log += msg; // Add to log
return await writeFile('app.log', log) // Write to log file
.catch(() => false) // Return false on error
.then(() => true); // Return true on success
};
// In your website's JS
// Get the config from the Node backend.
const success = await Gluon.ipc.log('Message!');
success // true
Store
Easily store common data between the Node backend and Web frontend in both directions. Values must be JSON serializable, and re-set to update the value.
// In your Node backend
import * as Gluon from '@gluon-framework/gluon';
const Window = await Gluon.open('https://gluonjs.org');
// Store common config in IPC Store
Window.ipc.store.config = {
env: 'production'
};
// In your website's JS
// Get the config using IPC Store
const { config } = Gluon.ipc.store;
config.env // 'production'
Events
Gluon’s IPC uses an asynchronous event-based system which you can also use. It’s recommended you use other sub-APIs where appropriate instead of using Events, as they are easier to use for specific uses. The same example above would look like this using events instead.
// In your Node backend
import * as Gluon from '@gluon-framework/gluon';
const Window = await Gluon.open('https://gluonjs.org');
// Expose our own function to read a config JSON file.
import { readFile } from 'fs/promises';
Window.ipc.on('get config', async () => JSON.parse(await readFile('config.json', 'utf8')));
// In your website's JS
// Get the config from the Node backend.
const config = await Gluon.ipc.send('get config');
Using in the Web frontend
The IPC API is near-identical in the Web frontend to ease development and simplify code. The main difference is there is no Expose API, instead you can call functions exposed via it by doing Window.ipc[key]()
.