Skip to content

Commit

Permalink
feature(example): add rpc-websockets example
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj committed Aug 31, 2023
1 parent 17a7df8 commit 4709a5d
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock.json
24 changes: 24 additions & 0 deletions examples/rpc-websockets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Deepkit RPC WebSockets Example

This example shows how to use the Deepkit RPC over WebSockets.

## How to run

First install dependencies

```
# postinstall script automatically installs deepkit type compiler
npm install
```

Start server:

```
./node_modules/.bin/ts-node server.ts
```

Start client:

```
./node_modules/.bin/ts-node client.ts
```
14 changes: 14 additions & 0 deletions examples/rpc-websockets/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { RpcWebSocketClient } from '@deepkit/rpc';
import type { Controller } from './server';

async function main() {
const client = new RpcWebSocketClient('ws://127.0.0.1:8081');
const controller = client.controller<Controller>('/main');

const result = await controller.hello('World');
console.log('result', result);

client.disconnect();
}

main().catch(console.error);
18 changes: 18 additions & 0 deletions examples/rpc-websockets/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "deepkit-example-rpc-websockets",
"version": "0.0.1",
"scripts": {
"postinstall": "deepkit-type-install"
},
"dependencies": {
"ws": "^8.13.0",
"@deepkit/type": "^1.0.1-alpha.97",
"@deepkit/rpc": "^1.0.1-alpha.97",
"@deepkit/rpc-tcp": "^1.0.1-alpha.97"
},
"devDependencies": {
"@deepkit/type-compiler": "^1.0.1-alpha.97",
"typescript": "^5.2.2",
"ts-node": "^10.9.1"
}
}
19 changes: 19 additions & 0 deletions examples/rpc-websockets/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { rpc, RpcKernel } from '@deepkit/rpc';
import { RpcWebSocketServer } from '@deepkit/rpc-tcp';

@rpc.controller('/main')
export class Controller {
@rpc.action()
hello(title: string): string {
return 'Hello ' + title;
}
}

const kernel = new RpcKernel();
kernel.registerController(Controller);
const server = new RpcWebSocketServer(kernel, 'localhost:8081');
server.start({
host: '127.0.0.1',
port: 8081,
});
console.log('Server started at ws://127.0.0.1:8081');
11 changes: 11 additions & 0 deletions examples/rpc-websockets/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"strict": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"target": "ES2022",
"esModuleInterop": true,
"module": "CommonJS"
},
"reflection": true
}

0 comments on commit 4709a5d

Please sign in to comment.