1.0.11 • Published 3 years ago

@hoobs/ipc v1.0.11

Weekly downloads
-
License
GPL-3.0
Repository
github
Last release
3 years ago

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.

variableRequireddescription
idYesa unique name for the socket
rootNothe path to store the Unix sock file, default is the app root
namespaceNoadds a domain to the sock files
encodingNotext encoding for socket messages, default is utf8
maxConnectionsNothis 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.

1.0.9

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago