Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/mattijsf/tango-rpc
- Owner: mattijsf
- License: mit
- Created: 2023-05-29T20:30:26.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-05-30T10:52:10.000Z (over 1 year ago)
- Last Synced: 2025-02-03T19:40:18.812Z (7 days ago)
- Topics: rpc, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/tango-rpc
- Size: 165 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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
})
```