Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/manga-download/websocket-rpc
A typesafe RPC implementation for WebSockets
https://github.com/manga-download/websocket-rpc
rpc websocket
Last synced: 3 months ago
JSON representation
A typesafe RPC implementation for WebSockets
- Host: GitHub
- URL: https://github.com/manga-download/websocket-rpc
- Owner: manga-download
- Created: 2022-12-18T15:34:31.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-27T14:18:19.000Z (6 months ago)
- Last Synced: 2024-10-20T03:44:56.772Z (4 months ago)
- Topics: rpc, websocket
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/websocket-rpc
- Size: 396 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# WebSocket RPC
A typesafe RPC implementation for WebSockets based on the popular [ws](https://www.npmjs.com/package/ws) package.
## Pre-Conditions
- The WebSocket server must be run in a NodeJS environment
- The WebSocket client should be run in a Browser environment## Install
### [ℹ️](https://www.pluralsight.com/resources/blog/guides/install-npm-packages-from-gitgithub) From GitHub
- [Latest](https://github.com/manga-download/websocket-rpc): `npm install github:manga-download/websocket-rpc`
- [Specific version](https://github.com/manga-download/websocket-rpc/tags): `npm install github:manga-download/websocket-rpc#v1.2.4`### From NPM
- [Latest](https://www.npmjs.com/package/websocket-rpc): `npm install websocket-rpc`
- [Specific version](https://www.npmjs.com/package/websocket-rpc?activeTab=versions): `npm install [email protected]`## Quick Start
### Contract
The contract is a class providing all methods on the RPC server that can be invoked remotely with the RPC client.
All methods must fulfill the following criterias:
- Must be async
- Must be void or return a JSON serializable result
- Must have zero or more parameters, each must be JSON serializable_MyContract.ts_
```typescript
import type { RemoteContract } from 'websocket-rpc';export class MyContract implements RemoteContract {
async Divide(a: number, b: number): Promise {
if(b === 0) {
throw new Error('Division by zero!');
} else {
return a / b;
}
}
}
```### Server
Start a WebSocket server which exposes the RPC methods from `MyContract`.
_MyServer.ts_
```typescript
import { CreateServer } from 'websocket-rpc/server';
import { MyContract } from './MyContract';const contract = new MyContract();
const wsConnectionInfo = { path: '/rpc', port: 8080 };// The server is the same as from the 'ws' package, but extended with RPC capabilities
const server = await CreateServer(contract, wsConnectionInfo);
```### Client
Connect to a WebSocket server and invoke the RPC methods from `MyContract`.
_MyClient.ts_
```typescript
import { CreateClient } from 'websocket-rpc/client';
import type { MyContract } from './MyContract';const client = await CreateClient('http://localhost:8080/rpc');
// TODO: Ensure the client is properly connected before invoking calls...
setTimeout(async () => {
// All defined methods from the contract can now be invoked via the RPC property
const result = await client.RPC.Divide(19, 7);
}, 1000);
```