3.0.3 • Published 4 months ago

rpc-with-types v3.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

rpc-with-types

An experimental library that implements a bidirectional Method+Event protocol for your homogenous Typescript code.

Here is a short example how it works:

// { server, client }

// Define a Method and an Event
const FindUser = Method.new("GetUser", t.string, t.type({ name: t.string, age: t.number }));
const NewUser = Event.new("NewUser", t.string);

// Implement a Method on the Server
server.onMethod(FindUser, name => { name: "Alice", age: 80000 });
server.onEvent(NewUser, name => console.log(`New user: ${name}`));

// Call it from the Client
const { name, age } = await (new FindUser("Alice").with(client));
new NewUser("Hashelq").with(client)

What is interesting, that everything you have just seen can also be used backwards!

client.onMethod(FindUser, name => { name: "???", age: -1 });
client.onEvent(NewUser, name => console.log(`New user: ${name}`));

const { name, age } = await (new FindUser("Alice").withs(server, server.clients[0]));
new NewUser("HashElq").withs(client, server.clients[0]);

How it works

The RPC works on top of the WebSockets protocol.

A human readable JSON text is sent between the server and client sides.

Compatibility

To run a server, you need to have an appropriate nodejs-like runtime.

The client side can work in a modern browser (thanks isomorphic-ws) and a nodejs-like runtime.

Sessions

interface Session {
  lastMessage: string
};

const server = new Server<Session>({ sessionInit: () => { lastMessage: "" } /* other params */});

server.onEvent(..., (message: string, { _socket, session } => {
  session.lastMessage = message;
}))

Links to understand how to deal with it

3.0.3

4 months ago

1.2.0

6 months ago

1.1.1

6 months ago

1.1.0

6 months ago

1.1.6

6 months ago

1.1.5

6 months ago

1.1.4

6 months ago

1.1.3

6 months ago

1.1.2

6 months ago

2.0.3

6 months ago

2.0.2

6 months ago

2.0.1

6 months ago

2.0.0

6 months ago

3.0.2

6 months ago

3.0.1

6 months ago

3.0.0

6 months ago

1.0.8

7 months ago

1.0.7

7 months ago

1.0.6

7 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago