Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mattijsf/tango-rpc

This library provides an easy and type-safe method for remote procedure calls (RPCs) over a string-based communication channel in TypeScript.
https://github.com/mattijsf/tango-rpc

rpc typescript

Last synced: 5 days ago
JSON representation

This library provides an easy and type-safe method for remote procedure calls (RPCs) over a string-based communication channel in TypeScript.

Awesome Lists containing this project

README

        

# tango-rpc

TypeScript-based Remote Procedure Call (RPC) library that is almost too simple.

## Key Features

- Typescript
- No dependencies
- Proxy-based client
- Ultra simple string-based `Channel` interface:
```typescript
interface Channel = {
sendMessage(message: string): void
addMessageListener(listener: (message: string) => void): void
removeMessageListener(listener: (message: string) => void): void
}
```
- Support for methods with on or more callback parameters
- Support for subscription/event callbacks
- Server-side error handling
- Lacks most other features

## Installation

```sh
npm install tango-rpc
```
or
```sh
yarn add tango-rpc
```

## Usage

To use this library, you need to define an API interface and supply the API implementation to the server. You'll also need to provide a `Channel` implementation which depends on your use case.

See [tango-rpc.test.ts](src/__tests__/tango-rpc.test.ts) for example usage.

```typescript
interface MyAPI {
add(a: number, b: number): Promise;
greet(name: string): Promise;
processItems(items: string[], callback: (processedItem: string) => void): Promise;
subscribeToEvents(callback: (event: string) => void): Promise;
triggerEvent(event: string): Promise;
errorProne(): Promise;
}

class MyAPIServer implements MyAPI {
// Implement your API methods here...
}
```

You need to instantiate a `Server` and `Client` with your API and channel.

```typescript
const testChannel = new TestChannel();
const myAPIServer = new MyAPIServer();
const server = new Server(testChannel, myAPIServer);
const client = new Client(testChannel);
```

You can use the client's proxy to call API methods as if they were local.

```typescript
const myAPIClient = client.proxy;
myAPIClient.add(1, 2).then(result => console.log(`1 + 2 = ${result}`));
myAPIClient.greet('World').then(result => console.log(result));
myAPIClient.processItems(['apple', 'banana', 'cherry'], item => console.log(`Processed item: ${item}`));
myAPIClient.subscribeToEvents(event => console.log(`Received event: ${event}`));
myAPIClient.triggerEvent('Test event');
myAPIClient.errorProne().catch(error => console.log(`Caught error: ${error.message}`));
```

In case you need to wait until the the server & client are ready you can use the client's `onConnect` event which ensures that the proxy is ready for interaction:

```typescript
client.onConnect(() => {
console.log(client.isConnected) // true
})
```