acebase-ipc-server
Version:
IPC Server that provides communication between isolated AceBase processes using the same database files, such as local pm2 and cloud-based clusters.
68 lines (67 loc) • 3.32 kB
TypeScript
import uWS from 'uWebSockets.js';
export interface AceBaseIPCServerConfig {
host?: string;
port: number;
/** Used to check if connections made to this server are using the right database */
/** Provide SSL certificate details. Use either `certPath` and `keyPath`, or `pfxPath` and `passphrase` */
ssl?: {
certPath?: string;
keyPath?: string;
pfxPath?: string;
passphrase?: string;
};
/**
* Maximum amount of bytes allowed to be sent over the websocket connection. The websocket connection is closed
* immediately if the payload exceeds this number. Default is 16KB (16384).
* Clients should send messages with larger content over http(s) with POST `/[dbname]/send?id=[clientId]`;
* to receive large messages the server will send `get:[msgId]` to the client over the websocket
* connection, the message can then be downloaded by calling GET `/[dbname]/receive?id=[clientId]&msgId=[id]` */
maxPayload?: number;
/** secret token to (help) prevent unauthorized clients to use the IPC channel */
token?: string;
}
interface AceBaseIPCClient {
id: string;
dbname: string;
connected: Date;
ws: uWS.WebSocket;
sendMessage(message: any): Promise<void>;
}
/**
* This flow is used for remote IPC communications
* Handshake:
* - Remote client connects to the websocket on url `/[dbname]/connect?id=[clientId]&v=[clientVersion]&t=[token]`
* - IPC Server adds it to the `clients` list for that dbname, if another client with the same id exists already, it's previous connection will be closed
* - IPC Server sends `"welcome:{ maxPayload: [maxPayload] }"` to the client to notify the maxPayload size to use
* - IPC Server broadcasts `"connect:clientid"`
*
* Connection checks:
* - To check the connection, client can send a `"ping"` message, which will immediately be replied to with `"pong"`
*
* Message sending:
* - If a client wants to send a message to 1 specific peer, it should prefix the message with `"to:[peerId];"`
* - Messages without prefix are broadcast to all other peers
* - If a message is prefixed `"to:all;"` the message will be sent to all other peers individually, this is provided for testing only - use unprefixed instead
* - If the message to send exceeds the configured payload size, it must be http(s) POSTed to "/send?id=[clientId]&t=[token]" instead
*
* Message receiving:
* - Clients receive small messages through the websocket connection.
* - Messages sent from other peers will be prefixed with `"msg:"`
* - Messages too large to be sent over the websocket connection, will send `"get:[msgId]"` instead, client must http(s) GET `"/[dbname]/receive?id=[clientId]&msg=[msgId]&t=[token]"` to download the message
*
* Disconnect:
* - Upon disconnection of a remote peer, server broadcasts `"disconnect:clientid"` to all still connected
*
*/
export declare class AceBaseIPCServer {
private config;
private clients;
constructor(config: AceBaseIPCServerConfig);
getClients(dbname: string): AceBaseIPCClient[];
start(): Promise<void>;
largeMessages: {
[id: string]: string;
};
handleIncomingMessage(msg: string, ws: uWS.WebSocket): boolean | undefined;
}
export {};