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
> Just looking for sample code? Check out the sources for the example [coffee machine server](./ts/bin/panrpc-example-websocket-coffee-server-cli.ts) and [coffee machine client/remote control](./ts/bin/panrpc-example-websocket-coffee-client-cli.ts).
420
+
421
+
#### 1. Choosing a Transport and Serializer
422
+
423
+
- Supports multiple transports
424
+
- Common ones are TCP, WebSockets, UNIX sockets or WebRTC
425
+
- Really anything with that exposes a stream-like interface works with the `LinkStream` (see [examples](#examples))
426
+
- For message-based transports (for use in message brokers like Redis or NATS) the `LinkMessage` API can also be used (see [examples](#examples))
427
+
- For this example, we'll use WebSockets
428
+
- Multiple serializers are supported
429
+
- The only requirement is that it supports streaming encode/decode
430
+
- Common ones are JSON and CBOR (see see [examples](#examples))
431
+
- For this example, we'll use JSON
432
+
433
+
#### 2. Creating a Server
434
+
435
+
- For this example we're creating a coffee machine server
436
+
- Creating the service with the RPCs (simple unary `BrewCoffee` RPC that simulates brewing coffee by waiting for 5 seconds before returning the new water level)
437
+
- Note how errors can simply be thrown and how no manual serialization needs to happen
438
+
- Adding the service to the RPC registry
439
+
- Creating a WebSocket server
440
+
- Creating a JSON encoder/decoder stream
441
+
- Passing the encoder/decoder streams to `registry.linkStream`
442
+
- Starting the server and watching it listen
443
+
444
+
#### 3. Creating a Client
445
+
446
+
- We're creating a remote control CLI that can control the coffee machine server
447
+
- Client will simply call the `BrewCoffee` function
448
+
- Defining the remote coffee machine server's RPCs
449
+
- Adding the remote RPCs to the RPC registry
450
+
- Creating a WebSocket client
451
+
- Creating a JSON encode/decoder stream
452
+
- Passing the encoder/decoder streams to `registry.linkStream`
453
+
- Starting the client and watching it connect
454
+
455
+
#### 4. Calling the Server's RPCs from the Client
456
+
457
+
- Client and server are connected, but we aren't calling the RPCs yet
458
+
- We'll create a simple TUI that will read a letter from `stdin` and brew a specific variant and size of coffee
459
+
- We'll then print the remaining water level back to the terminal
460
+
- Starting the client again, pressing "a", watching the new level being returned to the terminal
461
+
462
+
#### 5. Calling the Client's RPCs from the Server
463
+
464
+
- So far we've added RPCs to the server and called them from the client, like with most RPC frameworks
465
+
- Now we'll add RPCs to the client that the server can call
466
+
- This RPC will be called on all remote controls that are connected to the coffee machine to let them know that the coffee machine is currently brewing coffee
467
+
- We'll start by creating a service with a `SetCoffeeMachineBrewing` RPC that the client will expose (simply logs to the console)
468
+
- Add this service to the client's registry
469
+
- On the server's `BrewCoffee` RPC, before brewing coffee and after finishing, we'll call `SetCoffeeMachine` RPC for all clients that are connected (except the one that is calling the RPC itself - we can do that by checking the client's ID) with `this.forRemotes?`
470
+
- We can set `forRemotes` by getting it from the registry
471
+
- Starting the server again and starting three clients, then pressing "a" on one of them, and watching the other connected clients logging the brewing state before it has started/after it has stopped brewing except on the client that requested coffee to be brewed
472
+
473
+
#### 6. Passing Closures to RPCs
474
+
475
+
- In addition to calling RPCs on the client from the server and vice-versa, we can also pass functions (closures) to RPCs as arguments like with local functions and call them on the other end like if they were local
476
+
- We'll use this to report back progress of the coffee brewing process to the client while the `BrewCoffee` RPC is being called instead of just returning after it is done
477
+
- Add `onProgress` argument to remote `BrewCoffee` service on coffee machine server and decorate it with `@remoteClosure` to let `panrpc` know that it should be a closure
478
+
- Call the `onProgress` closure during the call to report progress
479
+
- On the client, extend the remote coffee machine service with the new remote closure argument, and pass along an anonymous function that logs the progress to `stdout`
480
+
- Restart the server and client
481
+
- Press "a", instead of just returning the water level after it's done it will stream in the progress as a percentage one by one
482
+
483
+
🚀 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