Browse Source

document event subscriptions

Refs #1100
pull/1103/head
Anton Kaliaev 7 years ago
parent
commit
b40aa91b41
No known key found for this signature in database GPG Key ID: 7B6881D965918214
1 changed files with 69 additions and 6 deletions
  1. +69
    -6
      rpc/core/events.go

+ 69
- 6
rpc/core/events.go View File

@ -13,11 +13,57 @@ import (
// Subscribe for events via WebSocket. // Subscribe for events via WebSocket.
// //
// To tell which events you want, you need to provide a query. query is a
// string, which has a form: "condition AND condition ..." (no OR at the
// moment). condition has a form: "key operation operand". key is a string with
// a restricted set of possible symbols ( \t\n\r\\()"'=>< are not allowed).
// operation can be "=", "<", "<=", ">", ">=", "CONTAINS". operand can be a
// string (escaped with single quotes), number, date or time.
//
// Examples:
// tm.event = 'NewBlock' # new blocks
// tm.event = 'CompleteProposal' # node got a complete proposal
// tm.event = 'Tx' AND tx.hash = 'XYZ' # single transaction
// tm.event = 'Tx' AND tx.height = 5 # all txs of the fifth block
// tx.height = 5 # all txs of the fifth block
//
// Tendermint provides a few predefined keys: tm.event, tx.hash and tx.height.
// Note for transactions, you can define additional keys by providing tags with
// DeliverTx response.
//
// DeliverTx{
// Tags: []*KVPair{
// "agent.name": "K",
// }
// }
//
// tm.event = 'Tx' AND agent.name = 'K'
// tm.event = 'Tx' AND account.created_at >= TIME 2013-05-03T14:45:00Z
// tm.event = 'Tx' AND contract.sign_date = DATE 2017-01-01
// tm.event = 'Tx' AND account.owner CONTAINS 'Igor'
//
// See list of all possible events here
// https://godoc.org/github.com/tendermint/tendermint/types#pkg-constants
//
// For complete query syntax, check out
// https://godoc.org/github.com/tendermint/tmlibs/pubsub/query.
//
// ```go // ```go
// import "github.com/tendermint/tmlibs/pubsub/query"
// import "github.com/tendermint/tendermint/types" // import "github.com/tendermint/tendermint/types"
// //
// client := client.NewHTTP("tcp://0.0.0.0:46657", "/websocket") // client := client.NewHTTP("tcp://0.0.0.0:46657", "/websocket")
// result, err := client.AddListenerForEvent(types.EventStringNewBlock())
// ctx, cancel := context.WithTimeout(context.Background(), timeout)
// defer cancel()
// query := query.MustParse("tm.event = 'Tx' AND tx.height = 3")
// txs := make(chan interface{})
// err := client.Subscribe(ctx, "test-client", query, txs)
//
// go func() {
// for e := range txs {
// fmt.Println("got ", e.(types.TMEventData).Unwrap().(types.EventDataTx))
// }
// }()
// ``` // ```
// //
// > The above command returns JSON structured like this: // > The above command returns JSON structured like this:
@ -35,7 +81,7 @@ import (
// //
// | Parameter | Type | Default | Required | Description | // | Parameter | Type | Default | Required | Description |
// |-----------+--------+---------+----------+-------------| // |-----------+--------+---------+----------+-------------|
// | event | string | "" | true | Event name |
// | query | string | "" | true | Query |
// //
// <aside class="notice">WebSocket only</aside> // <aside class="notice">WebSocket only</aside>
func Subscribe(wsCtx rpctypes.WSRPCContext, query string) (*ctypes.ResultSubscribe, error) { func Subscribe(wsCtx rpctypes.WSRPCContext, query string) (*ctypes.ResultSubscribe, error) {
@ -68,10 +114,8 @@ func Subscribe(wsCtx rpctypes.WSRPCContext, query string) (*ctypes.ResultSubscri
// Unsubscribe from events via WebSocket. // Unsubscribe from events via WebSocket.
// //
// ```go // ```go
// import 'github.com/tendermint/tendermint/types'
//
// client := client.NewHTTP("tcp://0.0.0.0:46657", "/websocket") // client := client.NewHTTP("tcp://0.0.0.0:46657", "/websocket")
// result, err := client.RemoveListenerForEvent(types.EventStringNewBlock())
// err := client.Unsubscribe("test-client", query)
// ``` // ```
// //
// > The above command returns JSON structured like this: // > The above command returns JSON structured like this:
@ -89,7 +133,7 @@ func Subscribe(wsCtx rpctypes.WSRPCContext, query string) (*ctypes.ResultSubscri
// //
// | Parameter | Type | Default | Required | Description | // | Parameter | Type | Default | Required | Description |
// |-----------+--------+---------+----------+-------------| // |-----------+--------+---------+----------+-------------|
// | event | string | "" | true | Event name |
// | query | string | "" | true | Query |
// //
// <aside class="notice">WebSocket only</aside> // <aside class="notice">WebSocket only</aside>
func Unsubscribe(wsCtx rpctypes.WSRPCContext, query string) (*ctypes.ResultUnsubscribe, error) { func Unsubscribe(wsCtx rpctypes.WSRPCContext, query string) (*ctypes.ResultUnsubscribe, error) {
@ -106,6 +150,25 @@ func Unsubscribe(wsCtx rpctypes.WSRPCContext, query string) (*ctypes.ResultUnsub
return &ctypes.ResultUnsubscribe{}, nil return &ctypes.ResultUnsubscribe{}, nil
} }
// Unsubscribe from all events via WebSocket.
//
// ```go
// client := client.NewHTTP("tcp://0.0.0.0:46657", "/websocket")
// err := client.UnsubscribeAll("test-client")
// ```
//
// > The above command returns JSON structured like this:
//
// ```json
// {
// "error": "",
// "result": {},
// "id": "",
// "jsonrpc": "2.0"
// }
// ```
//
// <aside class="notice">WebSocket only</aside>
func UnsubscribeAll(wsCtx rpctypes.WSRPCContext) (*ctypes.ResultUnsubscribe, error) { func UnsubscribeAll(wsCtx rpctypes.WSRPCContext) (*ctypes.ResultUnsubscribe, error) {
addr := wsCtx.GetRemoteAddr() addr := wsCtx.GetRemoteAddr()
logger.Info("Unsubscribe from all", "remote", addr) logger.Info("Unsubscribe from all", "remote", addr)


Loading…
Cancel
Save