This is an HTTP RMI implementation based on Fastify.
yarn add trmi-http
npm i trmi-http
You can find a more detailed example here.
Define and implement a remote service
type HelloResponse = {
message: string;
}
interface HelloWorldSpecification {
hello(world: string): Promise<HelloResponse>;
bye(): Promise<void>;
}
import { RemoteService, RemoteMethod } from 'trmi-http';
@RemoteService()
class HelloWorld implements HelloWorldSpecification {
@RemoteMethod()
async hello(world: string): Promise<HelloResponse> {
return {
message: `Hello ${world}!`,
};
}
@RemoteMethod()
async bye(): Promise<void> {
throw new Error('"bye" method is not implemented');
}
}
Start a remote service server
import { HttpRemoteServer } from 'trmi-http';
HttpRemoteServer.create({ port: 3478 })
.from(HelloWorld) // it is possible to pass varargs here
.start()
.catch(e => console.error('Failed to start a server', e));
Start a remote client
import { HttpRemoteClient } from 'trmi-http';
const client = HttpRemoteClient.create({ url: 'http://127.0.0.1:3478' }).start();
const helloWorld = client.getService<HelloWorldSpecification>('HelloWorld');
const response = await helloWorld.hello('world');
console.log(response.message);
helloWorld.bye().catch(console.log);
Remote service name
By default, the remote service name defaults to the class name. You can override this behaviour by passing a name property. Characters .~
are restricted as they are used in internal key generation.
@RemoteService({ name: 'MyServer_HelloWorld' })
class HelloWorld implements HelloWorldSpecification
client.getService<HelloWorldSpecification>('MyServer_HelloWorld');
Authorization
By default, server accepts requests from everywhere. You can change that by passing an AuthorizationProvider instance.
HttpRemoteServer.create({ authorization: TokenAuthorizationProvider.create('secret'), ... });
HttpRemoteClient.create({ authorization: TokenAuthorizationProvider.create('secret'), ... });
Server uses Fastify to receive requests from clients.
Client uses got to send requests.