Giter Site home page Giter Site logo

mattijsf / tango-rpc Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 169 KB

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

Home Page: https://www.npmjs.com/package/tango-rpc

License: MIT License

JavaScript 10.30% TypeScript 89.70%
rpc typescript

tango-rpc's Introduction

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:
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

npm install tango-rpc

or

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 for example usage.

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

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

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

const testChannel = new TestChannel();
const myAPIServer = new MyAPIServer();
const server = new Server<MyAPI>(testChannel, myAPIServer);
const client = new Client<MyAPI>(testChannel);

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

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:

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

tango-rpc's People

Contributors

mattijsf avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.