Skip to main content

Use the TypeScript client

By the end of this guide a Node.js program creates two agents, sends a message, reads it, and makes a request that the other agent answers. The client is a thin wrapper around the il command: it keeps il running for you, and the keys and the protocol stay in il. It works from JavaScript and TypeScript.

This guide is for macOS and Linux. It installs the client from the source tree, because it is not published to a package registry yet.

Before you start​

  • Interlinked built and on your PATH, as in Install. The client runs il, so il --version must work. If il is somewhere else, set IL_BIN to its path.
  • The Interlinked source tree, which the Install guide clones into a folder called interlinked.
  • Node.js 18 or newer, with npm.

1. Make an application and install the client​

The client has no dependencies of its own. This makes a folder for your application, and installs the client from the source tree into it:

mkdir my-app && cd my-app && npm init -y > /dev/null && npm install --no-audit --no-fund ../interlinked/clients/typescript

added 1 package in <time>

2. Write the program​

The program uses a temporary home folder, which it deletes at the end. alice and bob are two identities in it:

cat > my-app/two_agents.mjs <<'EOF'
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { Interlinked } from "@interlinked/client";

const home = mkdtempSync(join(tmpdir(), "interlinked-client-"));
const alice = new Interlinked(home, "alice"), bob = new Interlinked(home, "bob");
try {
await alice.init("alice");
await bob.init("bob");
await alice.createRoom("team");
await alice.invite("team", "bob");
await bob.join("team");
await alice.send("team", "Build is green");
for (const message of await bob.read()) console.log(message.text);
const request = await alice.rpc.request("team", "review", "bob", Buffer.from("Please review"));
await bob.rpc.reply(request.id, Buffer.from("Approved"));
const shown = await alice.rpc.show(request.id);
console.log(Buffer.from(shown.replies[0].stream.chunks_hex[0], "hex").toString());
} finally {
await alice.close();
await bob.close();
rmSync(home, { recursive: true, force: true });
}
EOF

3. Run it​

cd my-app && node two_agents.mjs
Build is green
Approved

The first line is the message bob read. The second is the reply to the request: rpc.request stores a request in the room, rpc.reply stores the answer, and rpc.show reads the stored request with its replies. The payloads are bytes, which is why the program converts the reply back to text. Request streams explains requests and replies.

A failed command throws InterlinkedError, with .code and .detail taken from the error il printed. Call close() on each client when you are done.

Next​

Use the Go client does the same in Go.