v 1.2.2
This commit is contained in:
@@ -38,11 +38,13 @@ client.connect().then(async () => {
|
||||
|
||||
# Using callbacks
|
||||
|
||||
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.
|
||||
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**.
|
||||
|
||||
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' }`.
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
You should unhook the client socket once you're done with it as not to cause security or program flow issues.
|
||||
|
||||
```typescript
|
||||
import {Backend, Frontend, Utils} from 'rpclibrary'
|
||||
@@ -56,7 +58,7 @@ const server = new Backend.RPCServer(20000, [{
|
||||
{
|
||||
name: 'subscribe',
|
||||
hook: async (callback) => {
|
||||
const resp = Utils.makeSubResponse() //Convenience method to generate SubscriptionResponse
|
||||
const resp:SubscriptionResponse = { result: 'Success', uuid: 'generate_a_random_string_here' }
|
||||
callbacks.set(resp.uuid, callback);
|
||||
return resp
|
||||
}
|
||||
@@ -85,18 +87,17 @@ If you need to include further response data into your `SubscriptionResponse` yo
|
||||
|
||||
```typescript
|
||||
new RPCServer<{extension: string}>({
|
||||
exportRPCs: () => [
|
||||
{
|
||||
name: 'MyRPCGroup',
|
||||
exportRPCs: () => [{
|
||||
name: 'subscribe',
|
||||
hook: async (callback) => {
|
||||
return {
|
||||
result: 'Success',
|
||||
uuid: 'very_random_string',
|
||||
extension: 'your_data_here'
|
||||
extension: 'your_data_here' //tsc will demand this field
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}]
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "rpclibrary",
|
||||
"version": "1.2.1",
|
||||
"version": "1.2.2",
|
||||
"description": "rpclibrary is a websocket on steroids!",
|
||||
"main": "./js/Index.js",
|
||||
"repository": {
|
||||
|
||||
Reference in New Issue
Block a user