@hoobs/ipc
v1.0.11
Published
A library for local inter process communications on unix based systems.
Downloads
39
Readme
IPC
A library for local interprocess communications on unix based systems.
Installation
You can install this module into your project.
yarn add @hoobs/ipc
Options
Options for creating an IPC server or client.
import { IPCOptions } from "@hoobs/ipc";
const options: IPCOptions = {
id: "test",
root: "./",
namespace: "example",
encoding: "utf8",
maxConnections: 100,
}
The example above creates a sock file example.test.sock
in the application's root.
| variable | Required | description | | -------------- | -------- | --------------------------------------------------------------------------------------- | | id | Yes | a unique name for the socket | | root | No | the path to store the Unix sock file, default is the app root | | namespace | No | adds a domain to the sock files | | encoding | No | text encoding for socket messages, default is utf8 | | maxConnections | No | this is the max number of connections allowed to a socket, default is 100 (server only) |
Server
Below is an example of a server implantation.
There are two types listeners;
- Event
- Route
Events are designed as void functions, where routes return a payload. Routes accept a path, parameters, and a body. Both requests and results are JSON objects.
import { IPCServer } from "@hoobs/ipc";
const server = new IPCServer({
id: "test",
maxConnections: 100,
});
// EVENTS
server.on("host", (data) => {
console.log(data);
});
// ROUTES
server.route("hello", (request, response) => {
response.send({
message: `Hello ${request?.params?.name}`,
});
});
server.start();
Client
The client can access a server's events and routes. Below is an example of a client implantation.
The two methods are;
- Emit
- Fetch
Emit sends a request to the server and doesn't expect a result. Fetch works much like HTTP calls. You need to define the route using the path parameter, and excepts parameters and a body object. Fetch is a promise, so it needs to be called with await or using the then method.
import { IPCClient } from "@hoobs/ipc";
const client = new IPCClient({
id: "test",
});
// EVENTS
client.emit("host", {
ip: "127.0.0.1",
});
// ROUTES
client.fetch("hello", {
name: "Luke Skywalker",
}).then((response) => {
console.log(response);
});
Legal
This is a modified project orginally wrote by Brandon Nozaki Miller.
HOOBS and the HOOBS logo are registered trademarks of HOOBS Inc. Copyright (C) 2021 HOOBS Inc. All rights reserved.