https://github.com/negrel/rpc
A lightweight, type-safe RPC facade and implementation for TypeScript.
https://github.com/negrel/rpc
Last synced: 10 months ago
JSON representation
A lightweight, type-safe RPC facade and implementation for TypeScript.
- Host: GitHub
- URL: https://github.com/negrel/rpc
- Owner: negrel
- License: mit
- Created: 2024-06-13T18:01:45.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-06-13T22:28:01.000Z (over 1 year ago)
- Last Synced: 2025-01-08T09:13:58.379Z (12 months ago)
- Language: TypeScript
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `rpc` - A lightweight, type-safe RPC facade and implementation for TypeScript.
A lightweight, type-safe RPC facade for typescript and a
[WebWorker](https://developer.mozilla.org/en-US/docs/Web/API/Worker) and HTTP
based implementations.
## Usage
### WebWorker
In `main.js`:
```js
import { WorkerRpcClient } from "jsr:@negrel/rpc";
// Create a worker based RpcClient.
const client = new WorkerRpcClient(
new URL("./worker_script.js", import.meta.url),
{ type: "module" },
);
// Remote Procedure Call.
const result = await client.remoteProcedureCall({
name: "doWork",
args: { i: Math.random() },
});
console.log(result);
// Stop worker.
client.terminate();
```
In `worker_script.js`:
```js
import { workerMessageHandler } from "jsr:@negrel/rpc";
self.onmessage = workerMessageHandler({
doWork({ i }) {
console.log("doWork", i);
return i * 2;
},
});
```
### HTTP
In `main.js`:
```js
import { HttpRpcClient } from "jsr:@negrel/rpc";
// Create a worker based RpcClient.
const client = new HttpRpcClient("http://localhost:8000");
// Remote Procedure Call.
const result = await client.remoteProcedureCall({
name: "doWork",
args: { i: Math.random() },
});
console.log(result);
```
In `server.js`:
```js
import { httpServerHandler } from "jsr:@negrel/rpc";
Deno.serve(httpServerHandler({
doWork({ i }) {
console.log("doWork", i);
return i * 2;
},
}));
```
## Contributing
If you want to contribute to `rpc` to add a feature or improve the code contact
me at [alexandre@negrel.dev](mailto:alexandre@negrel.dev), open an
[issue](https://github.com/negrel/rpc/issues) or make a
[pull request](https://github.com/negrel/rpc/pulls).
## :stars: Show your support
Please give a :star: if this project helped you!
[](https://www.buymeacoffee.com/negrel)
## :scroll: License
MIT © [Alexandre Negrel](https://www.negrel.dev/)