@slimio/tcp-sdk

SlimIO TCP Socket SDK. This package has been created to help developer to communicate with the production (Socket addon).

Usage no npm install needed!

<script type="module">
  import slimioTcpSdk from 'https://cdn.skypack.dev/@slimio/tcp-sdk';
</script>

README

Tcp-Sdk

SlimIO TCP Socket SDK. This package has been created to help developer to communicate with the production (Socket addon).

Requirements

  • Node.js v10 or higher

Getting Started

This package is available in the Node Package Repository and can be easily installed with npm or yarn.

$ npm i @slimio/tcp-sdk
# or
$ yarn add @slimio/tcp-sdk

Usage example

const TcpSdk = require("@slimio/tcp-sdk");
const CONNECT_TIMEOUT_MS = 1000;

async function main() {
    const client = new TcpSdk();
    await client.once("connect", CONNECT_TIMEOUT_MS);

    const info = await new Promise((resolve, reject) => {
        client.sendMessage("cpu.get_info").subscribe(resolve, reject);
    });
    console.log(info);

    client.close();
}

API

constructor(options?: TcpClient.ConstructorOptions)

Create and instanciate a new TCP Connection to the socket server. Listen for event connection to known when you'r ready to send messages.

Options is described by the following interface:

interface ConstructorOptions {
    host?: string;
    port?: number;
}

Default value of port would be TcpClient.DEFAULT_PORT.

getActiveAddons(): Promise< String[] >

Return the list of active addons on the current agent.

ping(): Promise< void >

Send a ping event (avoid timeout).

sendMessage(target: string, args?: any[]): ZenObservable.ObservableLike< any >

send a callback message.

sendOne< T >(target: string, args?: any[]): Promise< any >

send a callback message wrapped by a Promise.

connect(): Promise< void >

Connect (or re-connect) the Net.socket. If the Socket is alive it will return.

close(): void

Close the TCP (Socket) connection.

Properties

readonly agent(): TcpClient.AgentInfo | null

The readonly getter is described by the following TypeScript interface:

interface AgentInfo {
    location: string;
    version: string;
}

Dependencies

Name Refactoring Security Risk Usage
@slimio/safe-emitter Minor Medium Node.js Safe Emitter
zen-observable Minor Low Observable Implementation

License

MIT