|
@@ -38,11 +38,13 @@ client.connect().then(async () => {
|
38
|
38
|
|
39
|
39
|
# Using callbacks
|
40
|
40
|
|
41
|
|
-rpclibrary offers a special type of call that can be used with callbacks. The callback *has to be the last argument* and may be the only passed function.
|
|
41
|
+rpclibrary offers a special type of call that can be used with callbacks. The callback **has to be the last argument** and **may be the only passed function**.
|
42
|
42
|
|
43
|
43
|
In order to function, some metadata has to be included in the return value of hooks. On success, the function is expected to return a `type SubscriptionResponse = { result: 'Success', uuid: string }` or in case of errors a `type ErrorResposne = { result: 'Error' }`.
|
44
|
44
|
|
45
|
|
-The uuid, as the name implies, is used to uniquely identify the callback for a given invocation and also dictates the name given to the client-side RPC which you should unhook once you're done with it.
|
|
45
|
+The uuid, as the name implies, is used to uniquely identify the callback for a given invocation and also dictates the name given to the client-side RPC. Unless you got a preferred way of generating these (e.g. using some kind of unique information important to your task) we reccomend [uuid](https://www.npmjs.com/package/uuid) for this purpose.
|
|
46
|
+
|
|
47
|
+You should unhook the client socket once you're done with it as not to cause security or program flow issues.
|
46
|
48
|
|
47
|
49
|
```typescript
|
48
|
50
|
import {Backend, Frontend, Utils} from 'rpclibrary'
|
|
@@ -56,7 +58,7 @@ const server = new Backend.RPCServer(20000, [{
|
56
|
58
|
{
|
57
|
59
|
name: 'subscribe',
|
58
|
60
|
hook: async (callback) => {
|
59
|
|
- const resp = Utils.makeSubResponse() //Convenience method to generate SubscriptionResponse
|
|
61
|
+ const resp:SubscriptionResponse = { result: 'Success', uuid: 'generate_a_random_string_here' }
|
60
|
62
|
callbacks.set(resp.uuid, callback);
|
61
|
63
|
return resp
|
62
|
64
|
}
|
|
@@ -85,18 +87,17 @@ If you need to include further response data into your `SubscriptionResponse` yo
|
85
|
87
|
|
86
|
88
|
```typescript
|
87
|
89
|
new RPCServer<{extension: string}>({
|
88
|
|
- exportRPCs: () => [
|
89
|
|
- {
|
90
|
|
- name: 'subscribe',
|
91
|
|
- hook: async (callback) => {
|
92
|
|
- return {
|
93
|
|
- result: 'Success',
|
94
|
|
- uuid: 'very_random_string',
|
95
|
|
- extension: 'your_data_here'
|
96
|
|
- }
|
|
90
|
+ name: 'MyRPCGroup',
|
|
91
|
+ exportRPCs: () => [{
|
|
92
|
+ name: 'subscribe',
|
|
93
|
+ hook: async (callback) => {
|
|
94
|
+ return {
|
|
95
|
+ result: 'Success',
|
|
96
|
+ uuid: 'very_random_string',
|
|
97
|
+ extension: 'your_data_here' //tsc will demand this field
|
97
|
98
|
}
|
98
|
99
|
}
|
99
|
|
- ]
|
|
100
|
+ }]
|
100
|
101
|
})
|
101
|
102
|
|
102
|
103
|
```
|