Skip to content

IPC API

Trevor Sears edited this page Feb 23, 2020 · 5 revisions

Readying the API exposed by the main process

To register the various IPC handlers (the API, effectively) after each program start, just import registerHandlers from main/api/ipc-handler and call it:

import { registerHandlers } from "main/api/ipc-handler";
registerHandlers(window.webContents); // Handlers are now ready to go.

How the API Docs will be structured

For each 'method' available over the IPC (inter-process communication), there is a 'function name', a parameter type, and a return type. Note that the return type is always promisified (the resolved value of a Promise). In effect, using a given 'function' on the main process's IPC API would look something like:

let argument: ParameterType = /* something */;

// Calling "function-name" on the main process.
let result: ReturnType = await ipcRenderer.invoke("function-name", argument);

Listening for an event from the main process in the renderer process would look something like:

ipcRenderer.on("event-name", (eventInfo: EventInfoType): any => { /* Do stuff */ });

Invokable Functions

sign-up

// Not yet written
let argument: unknown = /* something */;
await ipcRenderer.invoke("sign-up", argument); // returns void

is-signed-in

let result: boolean = await ipcRenderer.invoke("is-signed-in", undefined);

sign-in

This must be done before calling any of the below commands.

let argument: { username: string, password: string } = /* something */;
await ipcRenderer.invoke("sign-in", argument); // returns void

send-message

let argument: { threadID: string, message: string } = /* something */;
let result: Message = await ipcRenderer.invoke("send-message", argument);

Typings for Message here.

create-conversation

// Not yet written
let argument: unknown = /* something */;
let result: unknown = await ipcRenderer.invoke("new-conversation", argument);

get-user-info

let result: UserInfo = await ipcRenderer.invoke("get-user-info", undefined);

UserInfo is an exported type in ipc-handler.ts.

search-users

let argument: string = /* something */;
let result: string[] = await ipcRenderer.invoke("search-users", argument);

set-user-avatar

let argument: { filePath: string } = /* something */;
await ipcRenderer.invoke("set-user-avatar", argument); // returns void

set-group-info

let argument: { name?: string, tagline?: string } = /* something */;
await ipcRenderer.invoke("set-group-info", argument); // returns void

Events

new-message

ipcRenderer.on("new-message", (message: Message): any => {
    // handle new message
});

Typings for Message here.

group-info-changed

ipcRenderer.on("group-info-changed", (info: { threadID: string, name: string, tagline: string }): any => {
    // handle group info changes
});