You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+24-10Lines changed: 24 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,13 +31,13 @@ It enables you to ...
31
31
32
32
### Library
33
33
34
-
You can add panrpc to your Go project by running the following:
34
+
You can add panrpc to your **Go** project by running the following:
35
35
36
36
```shell
37
37
go get github.com/pojntfx/panrpc/...@latest
38
38
```
39
39
40
-
For TypeScript, you can add panrpc to your project (both server-side TypeScript/Node.js and all major browser engines are supported) by running the following:
40
+
For **TypeScript**, you can add panrpc to your project (both server-side TypeScript/Node.js and all major browser engines are supported) by running the following:
41
41
42
42
```shell
43
43
npm i -s @pojntfx/panrpc
@@ -71,9 +71,14 @@ You can find binaries for more operating systems and architectures on [GitHub re
71
71
72
72
## Usage
73
73
74
+
### Go
75
+
76
+
<details>
77
+
<summary>Expand instructions</summary>
78
+
74
79
> TL;DR: Define the local and remote functions as struct methods, add them to a registry and link it with a transport
75
80
76
-
### 1. Define Local Functions
81
+
####1. Define Local Functions
77
82
78
83
panrpc uses reflection to create the glue code required to expose and call functions. Start by defining your server's exposed functions like so:
79
84
@@ -113,7 +118,7 @@ The following limitations on which functions you can expose exist:
113
118
- Functions can not have variadic arguments
114
119
- Functions must return either an error or a single value and an error
115
120
116
-
### 2. Define Remote Functions
121
+
####2. Define Remote Functions
117
122
118
123
Next, define the functions exposed by the client to the server using a struct without method implementations:
119
124
@@ -135,7 +140,7 @@ type remote struct {
135
140
}
136
141
```
137
142
138
-
### 3. Add Functions to a Registry
143
+
####3. Add Functions to a Registry
139
144
140
145
For the server, you can now create the registry, which will expose its functions:
Note the second generic parameter; it is the type that should be used for encoding nested messages. For JSON, this is typically `json.RawMessage`, for CBOR, this is `cbor.RawMessage`. Using such a nested message type is recommended, as it leads to a faster encoding/decoding since it doesn't require multiple encoding/decoding steps in order to function, but using `[]byte` (which will use multiple encoding/decoding steps) is also possible if this is not an option (for more infromation, see [Protocol](#protocol)).
169
174
170
-
### 4. Link the Registry to a Transport and Serializer
175
+
####4. Link the Registry to a Transport and Serializer
171
176
172
177
Next, expose the functions by linking them to a transport. There are two available transport APIs; the [Stream-Oriented API](https://pkg.go.dev/github.com/pojntfx/panrpc/pkg/rpc#LinkStream) (which is useful for stream-like transports such as TCP, WebSockets, WebRTC or anything else that provides an `io.ReadWriteCloser`), and the [Message-Oriented API](https://pkg.go.dev/github.com/pojntfx/panrpc/pkg/rpc#LinkMessage) (which is useful for transports that use messages, such as message brokers like Redis, UDP or other packet-based protocols). In this example, we'll use the stream-oriented API; for more information on using the m, meaning it can run in the browser!essage-oriented API, see [Examples](#examples).
173
178
@@ -268,7 +273,7 @@ if err := registry.LinkStream(
268
273
}
269
274
```
270
275
271
-
### 5. Call the Functions
276
+
####5. Call the Functions
272
277
273
278
Now you can call the functions exposed on the server from the client and vise versa. For example, to call `Println`, a function exposed by the client from the server:
Because panrpc is bidirectional, it is possible to pass closures and callbacks as function arguments, just like you would locally. For example, on the server:
🚀 That's it! We can't wait to see what you're going to build with panrpc.
410
+
🚀 That's it! We can't wait to see what you're going to build with panrpc. Be sure to take a look at the [reference](#reference) and [examples](#examples) for more information.
0 commit comments