Skip to content

Commit

Permalink
Writing #68: completed docs > features > websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
samchon committed May 15, 2024
1 parent 6c647a5 commit 93a2c48
Show file tree
Hide file tree
Showing 14 changed files with 475 additions and 126 deletions.
38 changes: 22 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# TGrid
![TGrid logo](https://private-user-images.githubusercontent.com/13158709/329786980-42e584e9-bede-4879-b416-627060a21ef6.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTU0MzY0NzIsIm5iZiI6MTcxNTQzNjE3MiwicGF0aCI6Ii8xMzE1ODcwOS8zMjk3ODY5ODAtNDJlNTg0ZTktYmVkZS00ODc5LWI0MTYtNjI3MDYwYTIxZWY2LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA1MTElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNTExVDE0MDI1MlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTU4OTEwNzEzMjc3NGExOWE2ZTM4OTdlYmViYjE1MTI2Y2VkN2FmMGM4ZDgxZWNkY2Q4MDE0Mjg0OGQ3ZGJhNmEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.TNb9oEvZpawGXAJ9heHAcp9jZMleFUK1SIK_PYZHYig)
![TGrid logo](https://tgrid.com/og.jpg)

[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/samchon/tgrid/blob/master/LICENSE)
[![npm version](https://badge.fury.io/js/tgrid.svg)](https://www.npmjs.com/package/tgrid)
Expand All @@ -9,7 +9,7 @@

TypeScript Grid Computing Framework.

TypeScript RPC (Remote Procedure Call) framework for WebSocket and Worker protocols.
TypeScript RPC (Remote Procedure Call) framework for `WebSocket` and `Worker` protocols.

- `WebSocket`
- `Worker`
Expand Down Expand Up @@ -42,25 +42,30 @@ export const webSocketClientMain = async () => {
await connector.connect("ws://127.0.0.1:37000/composite");

const remote: Driver<ICompositeCalculator> = connector.getDriver();
await remote.plus(10, 20); // returns 30
await remote.multiplies(3, 4); // returns 12
await remote.divides(5, 3); // returns 1.67
await remote.scientific.sqrt(2); // returns 1.41
await remote.statistics.mean(1, 3, 9); // returns 4.33
console.log(
await remote.plus(10, 20), // returns 30
await remote.multiplies(3, 4), // returns 12
await remote.divides(5, 3), // returns 1.67
await remote.scientific.sqrt(2), // returns 1.41
await remote.statistics.mean(1, 3, 9), // returns 4.33
);

await connector.close();
console.log(...stack);
console.log(stack);
};
```

> Execution result:
>
> [!TIP]
> ```bash
> { type: 'plus', input: [ 10, 20 ], output: 30 }
> { type: 'multiplies', input: [ 3, 4 ], output: 12 }
> { type: 'divides', input: [ 5, 3 ], output: 1.67 }
> { type: 'sqrt', input: [ 2 ], output: 1.41 }
> { type: 'mean', input: [ 1, 3, 9 ], output: 4.33 }
> $ npx ts-node examples/src/websocket
> 30 12 1.67 1.41 4.33
> [
> { type: 'plus', input: [ 10, 20 ], output: 30 },
> { type: 'multiplies', input: [ 3, 4 ], output: 12 },
> { type: 'divides', input: [ 5, 3 ], output: 1.67 },
> { type: 'sqrt', input: [ 2 ], output: 1.41 },
> { type: 'mean', input: [ 1, 3, 9 ], output: 4.33 }
> ]
> ```
Expand Down Expand Up @@ -100,7 +105,8 @@ Check out the document in the [website](https://tgrid.com/docs):
- [NestJS WebSocket SDK](https://tgrid.com/docs/examples/nestjs-websocket-sdk)
- Learn from Projects
- [Chat Application](https://tgrid.com/docs/projects/chat)
- [Grid Market](https://tgrid.com/docs/examples/market)
- [Grid Market](https://tgrid.com/docs/projects/market)
- [Mutex Server](https://tgrid.com/docs/projects/mutex)
### 🔗 Appendix
- [API Documents](https://tgrid.com/api)
3 changes: 2 additions & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "@samchon/tgrid-example-websocket",
"private": true,
"name": "@tgrid/examples",
"version": "1.0.0",
"description": "",
"main": "index.js",
Expand Down
14 changes: 8 additions & 6 deletions examples/src/nestjs/calculate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ export const testCalculateSdk = async () => {
listener,
);

await driver.plus(10, 20); // returns 30
await driver.multiplies(3, 4); // returns 12
await driver.divides(5, 3); // returns 1.67
await driver.scientific.sqrt(2); // returns 1.41
await driver.statistics.mean(1, 3, 9); // returns 4.33
console.log(
await driver.plus(10, 20), // returns 30
await driver.multiplies(3, 4), // returns 12
await driver.divides(5, 3), // returns 1.67
await driver.scientific.sqrt(2), // returns 1.41
await driver.statistics.mean(1, 3, 9), // returns 4.33
);

await connector.close();
console.log(...stack);
console.log(stack);
};
12 changes: 7 additions & 5 deletions examples/src/shared-worker/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ export const sharedWorkerClientMain = async () => {
await connector.connect("./server.js");

const remote: Driver<ICompositeCalculator> = connector.getDriver();
await remote.plus(10, 20); // returns 30
await remote.multiplies(3, 4); // returns 12
await remote.divides(5, 3); // returns 1.67
await remote.scientific.sqrt(2); // returns 1.41
await remote.statistics.mean(1, 3, 9); // returns 4.33
console.log(
await remote.plus(10, 20), // returns 30
await remote.multiplies(3, 4), // returns 12
await remote.divides(5, 3), // returns 1.67
await remote.scientific.sqrt(2), // returns 1.41
await remote.statistics.mean(1, 3, 9), // returns 4.33
);

await connector.close();
console.log(stack);
Expand Down
13 changes: 8 additions & 5 deletions examples/src/websocket/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ export const webSocketClientMain = async () => {
await connector.connect("ws://127.0.0.1:37000/composite");

const remote: Driver<ICompositeCalculator> = connector.getDriver();
await remote.plus(10, 20); // returns 30
await remote.multiplies(3, 4); // returns 12
await remote.divides(5, 3); // returns 1.67
await remote.scientific.sqrt(2); // returns 1.41
await remote.statistics.mean(1, 3, 9); // returns 4.33
console.log(
await remote.plus(10, 20), // returns 30
await remote.multiplies(3, 4), // returns 12
await remote.divides(5, 3), // returns 1.67
await remote.scientific.sqrt(2), // returns 1.41
await remote.statistics.mean(1, 3, 9), // returns 4.33
);

await connector.close();
console.log(stack);
};
12 changes: 7 additions & 5 deletions examples/src/worker/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ export const workerClientMain = async () => {
await connector.connect(`${__dirname}/server.${EXTENSION}`);

const remote: Driver<ICompositeCalculator> = connector.getDriver();
await remote.plus(10, 20); // returns 30
await remote.multiplies(3, 4); // returns 12
await remote.divides(5, 3); // returns 1.67
await remote.scientific.sqrt(2); // returns 1.41
await remote.statistics.mean(1, 3, 9); // returns 4.33
console.log(
await remote.plus(10, 20), // returns 30
await remote.multiplies(3, 4), // returns 12
await remote.divides(5, 3), // returns 1.67
await remote.scientific.sqrt(2), // returns 1.41
await remote.statistics.mean(1, 3, 9), // returns 4.33
);

await connector.close();
console.log(stack);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"Worker",
"SharedWorker",
"NestJS",
"nestia",
"tRPC",
"thread",
"process"
Expand Down
50 changes: 35 additions & 15 deletions website/pages/docs/features/components.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@ import { Tabs, Tab } from 'nextra-theme-docs'
import Alert from '@mui/material/Alert';
import AlertTitle from '@mui/material/AlertTitle';

![Sequence Diagram](/images/diagrams/sequence.png)
## Outline
This chapter describes key components of the `TGrid` only in the conceptual level.

If you're not familar with theoretical stories, it's okay to skip to the next chapter [Features > WebSocket Protocol](./websocket).

Otherwise, let's study about the key components of the `TGrid`.

- [`Communicator`](#communicator): network communication with remote system
- [`Provider`](#provider): object provided for remote system
- [`Listener`](#listener): interface of the remote system's `Provider`
- [`Driver`](#driver): proxy instance for calling functions of the remote system's `Provider`




## Communicator
![Sequence Diagram](/images/diagrams/sequence.png)

Communicates with a remote system.

`Communicator` is a class taking full responsibility to network communication with remote system. You can register a Provider, an object would be provided to the remote system, to the `Communicator`. Also, [Driver](#driver)\<[Listener](#listener)\>, which can access to the remote system's [Provider](#provider), is created by this `Communicator`.
Expand Down Expand Up @@ -104,24 +119,29 @@ export const webSocketClientMain = async () => {
await connector.connect("ws://127.0.0.1:37000/composite");

const remote: Driver<ICompositeCalculator> = connector.getDriver();
await remote.plus(10, 20); // returns 30
await remote.multiplies(3, 4); // returns 12
await remote.divides(5, 3); // returns 1.67
await remote.scientific.sqrt(2); // returns 1.41
await remote.statistics.mean(1, 3, 9); // returns 4.33
console.log(
await driver.plus(10, 20), // returns 30
await driver.multiplies(3, 4), // returns 12
await driver.divides(5, 3), // returns 1.67
await driver.scientific.sqrt(2), // returns 1.41
await driver.statistics.mean(1, 3, 9), // returns 4.33
);

console.log(...stack);
await connector.close();
console.log(stack);
};
```

> Execution result:
>
> ```bash
> { type: 'plus', input: [ 10, 20 ], output: 30 }
> { type: 'multiplies', input: [ 3, 4 ], output: 12 }
> { type: 'divides', input: [ 5, 3 ], output: 1.67 }
> { type: 'sqrt', input: [ 2 ], output: 1.41 }
> { type: 'mean', input: [ 1, 3, 9 ], output: 4.33 }
> ```bash filename="console"
> $ npx ts-node examples/src/websocket
> 30 12 1.67 1.41 4.33
> [
> { type: 'plus', input: [ 10, 20 ], output: 30 },
> { type: 'multiplies', input: [ 3, 4 ], output: 12 },
> { type: 'divides', input: [ 5, 3 ], output: 1.67 },
> { type: 'sqrt', input: [ 2 ], output: 1.41 },
> { type: 'mean', input: [ 1, 3, 9 ], output: 4.33 }
> ]
> ```
Driver of Listener for RPC (Remote Procedure Call).
Expand Down
Loading

0 comments on commit 93a2c48

Please sign in to comment.