Skip to content

Commit

Permalink
Update readme with new websocket usage patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
cmmarslender committed Mar 16, 2024
1 parent 15dba2b commit 0217181
Showing 1 changed file with 55 additions and 8 deletions.
63 changes: 55 additions & 8 deletions pkg/rpc/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func main() {

Websockets function asynchronously and as such, there are a few implementation differences compared to using the simpler HTTP request/response pattern. You must define a handler function to process responses received over the websocket connection, and you must also specifically subscribe to the events the handler should receive.

#### Handler Function
#### Handler Functions

Handler functions must use the following signature: `func handlerFunc(data *types.WebsocketResponse, err error)`. The function will be passed the data that was received from the websocket and an error.

Expand Down Expand Up @@ -106,13 +106,44 @@ func gotResponse(data *types.WebsocketResponse, err error) {
}
```

You may also use a blocking/synchronous handler function, if listening to websocket responses is all your main process is doing:
#### Synchronous Mode

If you want websockets to behave more like request/response style calls, you can enable sync mode.

To make all calls sync by default, you can set an option on the client:

```go
package main

import (
"log"
"fmt"

"github.com/chia-network/go-chia-libs/pkg/rpc"
)

func main() {
client, err := rpc.NewClient(rpc.ConnectionModeWebsocket, rpc.WithAutoConfig(), rpc.WithSyncWebsocket())
if err != nil {
// error happened
}

netInfo, _, err := client.DaemonService.GetNetworkInfo(&rpc.GetNetworkInfoOptions{})
if err != nil {
// error happened
}

// netInfo has the actual network information, since we're running in sync mode
fmt.Println(netInfo.NetworkName.OrEmpty())
}
```

You can also temporarily enable synchronous mode and then turn it back off

```go
package main

import (
"fmt"

"github.com/chia-network/go-chia-libs/pkg/rpc"
"github.com/chia-network/go-chia-libs/pkg/types"
Expand All @@ -121,19 +152,35 @@ import (
func main() {
client, err := rpc.NewClient(rpc.ConnectionModeWebsocket, rpc.WithAutoConfig())
if err != nil {
log.Fatalln(err.Error())
// error happened
}

client.ListenSync(gotResponse)
client.AddHandler(gotAsyncResponse)

// Other application logic here
client.SetSyncMode()

netInfo, _, err := client.DaemonService.GetNetworkInfo(&rpc.GetNetworkInfoOptions{})
if err != nil {
// error happened
}
fmt.Println(netInfo.NetworkName.OrEmpty())

client.SetAsyncMode()
}

func gotResponse(data *types.WebsocketResponse, err error) {
log.Printf("Received a `%s` command response\n", data.Command)
func gotAsyncResponse(data *types.WebsocketResponse, err error) {
log.Printf("Received a `%s` async command response\n", data.Command)
}
```

The output of this program will look something like the following. Note that both the async handler AND the sync response
variables saw the event and were able to handle it.

```shell
Received a `get_network_info` command response
mainnet
```

#### Subscribing to Events

There are two helper functions to subscribe to events that come over the websocket.
Expand Down

0 comments on commit 0217181

Please sign in to comment.