Skip to content

Commit

Permalink
chore: add rpc-websocket-esm example
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj committed Oct 3, 2023
1 parent c7d87c5 commit 00af116
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
24 changes: 24 additions & 0 deletions examples/rpc-websockets-esm/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-esm server.ts
```

Start client:

```
./node_modules/.bin/ts-node-esm client.ts
```
14 changes: 14 additions & 0 deletions examples/rpc-websockets-esm/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);
19 changes: 19 additions & 0 deletions examples/rpc-websockets-esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "deepkit-example-rpc-websockets",
"version": "0.0.1",
"type": "module",
"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-esm/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-esm/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": "esnext"
},
"reflection": true
}

0 comments on commit 00af116

Please sign in to comment.