-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonrpc.qmd
More file actions
136 lines (105 loc) · 2.45 KB
/
jsonrpc.qmd
File metadata and controls
136 lines (105 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
---
title: JSON-RPC
---
Rembus fully supports the
[JSON_RPC 2.0 Specification](https://www.jsonrpc.org/specification)
## RPC Request
```{mermaid}
flowchart LR
C(("JSON-RPC
Client")) --> S((Julia Server))
style C fill:green, color:white
style S fill:blue, color:white
```
### Server 🔵 - Exposing a service
The following (Julia) server exposes a method `mymethod` over an HTTP endpoint:
```julia
using Rembus
function mymethod(;x, y, z) # expect to be called with keywords arguments
return Dict(
"x" => x,
"y" => y,
"z" => z,
"op" => "x + y*z",
"value" => x + y * z
)
end
rb = component(http=9000)
expose(rb, mymethod)
wait(rb)
```
### JSON-RPC Client 🟢
When the server method requires keyword arguments, the JSON-RPC `params` field
must be an object with matching keys:
```bash
curl -X POST http://localhost:9000 \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "mymethod",
"params": {"x": 1, "y": 2, "z": 3}
}'
```
The server responds with a JSON object encoding the Julia `Dict` returned
by `mymethod`:
```json
{
"id":1,
"jsonrpc":"2.0",
"result": {
"x":1,
"y":2,
"z":3,
"op":"x + y*z",
"value":7
}
}
```
## Sending Notifications
A JSON_RPC **Notification** is simply a Request object **without an** `id`
field.
In Rembus, notifications map naturally to **Pub/Sub messages**: any published
message is delivered to all subscribed components.
In this example, the topic is named `mymethod`.
### Server 🔵 - Subscribing to a topic
```julia
using Rembus
function mymethod(x, y, z)
println("mymethod called with x=$x, y=$y, z=$z")
end
rb = component(http=9000)
subscribe(rb, mymethod)
# Declare readiness to receive messages from subscribed topics,
# in this case the topic mymethod.
reactive(rb)
# Start the event loop.
wait(rb)
```
### JSON-RPC Publisher 🟢
```bash
curl -X POST http://localhost:9000 \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "mymethod",
"params": [1,2,3]
}'
```
## Batch
To send multiple requests in a single call, the client may submit an array
of Request objects:
```bash
curl -X POST http://localhost:9000 \
-H "Content-Type: application/json" \
-d '[
{
"jsonrpc": "2.0",
"method": "method1",
"params": {"x": 1, "y": 2, "z": 3}
},{
"jsonrpc": "2.0",
"method": "method2",
"params": ["mystring", 1.0]
}
]'
```