Skip to main content

Client

Introduction

As opposed to the usage of multiple Event Emitters (such as ipcMain or ipcRenderer), the broker exposes a single BrokerClient class. Which acts as a universal message broadcaster on every process. It also does not accept multiple arguments as message payload, it uses one argument instead.

Building of BrokerClient object is done by the usage of Broker class method called createClient(). Each instance it returns is unique. Therefore, middleware bound to one instance, won't be present in another one.

index.tsx
import 'reflect-metadata';
import { BrokerClient, Broker } from 'electron-broker';

let broker: Broker;
let client: BrokerClient;

async function createBroker() {
broker = await BrokerFactory.createRendererBroker({
secure: true,
});

broker.start();

client = broker.createClient();
}

Send

Synchronously emits the message on a specific pattern, and does not listen for the response. It takes the pattern as the first argument and payload as the second one.

send.ts
function incrementByValue(): void {
const pattern = 'increment-by-value';
const payload = { value: 2136.99 };

broker.send(pattern, payload);
}
info

The send() method only triggers onRequest() part of Middleware interface.

Invoke

Asynchronously emits the message on a specific pattern, and listens for the response. It takes the pattern as the first argument and payload as the second one.

invoke.ts
async function checkWeather(): Promise<string> {
const pattern = 'check-weather';
const payload = { city: 'Warsaw' };

return broker.invoke(pattern, payload);
}

const weather = await checkWeather();

InvokeRaw

Asynchronously emits the message on a specific pattern, and listens for the response. But upon the response, it returns the BrokerEvent object instead of its data property, as in invoke() method.

invoke-raw.ts
async function checkWeatherRaw(): Promise<BrokerEvent> {
const pattern = 'check-weather';
const payload = { city: 'Warsaw' };

return broker.invokeRaw(pattern, payload);
}

const brokerEvent = await checkWeatherRaw();

// Access the results of the message
console.log(brokerEvent.data);

Subscribe

Creates the subscriber, which listens for messages on the specific pattern by usage listener function.

subscriber.ts
interface UserSessionDto {
username: string;
}

const oneTimeSubscriber = broker.subscribe<UserSessionDto>(
'user-session-start',
(data, brokerEvent) => { // brokerEvent arguement is optional.
console.log(`[LOG] Session for user ${data.username} has been initialised.`)

// Remove the subscription after receiving the message.
subscriber.unsubscribe();
}
)
info

Messages received through subscribers don't trigger the execution of middlewares.

SetMiddleware

Sets the middlewares, that will be executed after usage of send(), invoke(), and invokeRaw() methods.

middleware.ts
import {BrokerClient, Middleware, ExecutionContext} from 'electron-broker';

class MessageLogMiddleware implements Middleware {
public onRequest(context: ExecutionContext) {
const { eventId } = context.brokerEvent;

console.log(`[LOG] Received message with eventId: ${eventId}`)
}

public onResponse(context: ExecutionContext) {
const { eventId } = context.brokerEvent;

console.log(`[LOG] Sent message with eventId: ${eventId}`)
}
}

brokerClient.setMiddleware([new MessageLogMiddleware()])

SetTimeout

Sets the number of seconds that client will wait for the answer to the message, by default it fallbacks to 30 seconds.

timeout.ts
brokerClient.setTimeout(30);