# Subscribe
# Event Subscription
To address different scenarios, subscription through polling and callback are both provided in Vite as two options.
Polling API requires client program to create a filter first, then polls subscribe_getFilterChanges
method by filterId
for new events.
Remember, filter will expire if it has not been used for more than 5 minutes, in this case you should create a new filter for further subscription.
You can also manually un-subscribe by calling subscribe_uninstallFilter
method.
subscribe_newSnapshotBlocksFilter
, subscribe_newAccountBlocksFilter
, subscribe_newAccountBlocksByAddrFilter
, subscribe_newOnroadBlocksByAddrFilter
, subscribe_newLogsFilter
, subscribe_uninstallFilter
and subscribe_getFilterChanges
are polling APIs.
Callback API registers new subscription through WebSocket connection. Afterwards, subscribed events will be returned in callbacks when they are generated. This kind of subscription will close automatically when the WebSocket connection is closed.
subscribe_newSnapshotBlocks
, subscribe_newAccountBlocks
, subscribe_newAccountBlocksByAddr
, subscribe_newOnroadBlocksByAddr
and subscribe_newLogs
are callback APIs.
5 kinds of events are supported: new snapshot event, new transaction event, new transaction event on certain account, un-received transaction event on certain account and new log event.
Event supports rollback. When rollback occurs, removed
field in the event content is set to true.
To enable event subscription, you should
- Install a minimum 2.1.4 version of 'gvite' software
- Add
"subscribe"
into"PublicModules"
and set"SubscribeEnabled":true
in node_config.json
# Usage Example
# Subscribe via Callback
First, register a new subscription on address vite_f48f811a1800d9bde268e3d2eacdc4b4f8b9110e017bd7a76f
by calling subscribe_subscribe
method with name newLogs
.
This will return a new subscription id 0x4b97e0674a5ebef942dbb07709c4a608
New logs will be sent to subscriber in callback's result
field when generated
Subscription under callback is not necessarily cancelled explicitly, but will end once the connection is closed.
# Subscribe via Polling
Create a log filter on address vite_f48f811a1800d9bde268e3d2eacdc4b4f8b9110e017bd7a76f
This will return a new filter id 0x61d780619649fb0872e1f94a40cec713
Now poll for new events periodically by filter id
New logs generated after last poll will be returned
You should cancel subscription by calling subscribe_uninstallFilter
explicitly under polling mode
# Log Completion
Accidental connection interrupt may cause subscription closed. Under this situation, historical snapshot blocks, account blocks or un-received blocks can be fetched by ledger_getSnapshotBlocks
, ledger_getBlocksByHeight
or onroad_getOnroadBlocksByAddress
, while historical logs can be retrieved by subscribe_getLogs
method.
For example, in order to re-subscribe to new snapshot event, you should call subscribe_newSnapshotBlocks
to resume subscription first, then obtain latest snapshot block height by ledger_getSnapshotChainHeight
, finally call ledger_getSnapshotBlocks
to get all snapshot blocks missed.
# subscribe_newSnapshotBlocksFilter
Create a filter for polling for new snapshot blocks by passing into subscribe_getFilterChanges
as parameter
- Returns:
string
filterId
# subscribe_newAccountBlocksFilter
Create a filter for polling for new transactions on all accounts by passing into subscribe_getFilterChanges
as parameter
- Returns:
string
filterId
# subscribe_newAccountBlocksByAddrFilter
Create a filter for polling for new transactions on specified account by passing into subscribe_getFilterChanges
as parameter
Parameters:
string address
: Address of account
Returns:
string
filterId
# subscribe_newOnroadBlocksByAddrFilter
Create a filter for polling for un-received transactions on specified account by passing into subscribe_getFilterChanges
as parameter.
The events include new un-received transaction, transaction is received and un-received transaction is rolled back.
Parameters:
string address
: Address of account
Returns:
string
filterId
# subscribe_newLogsFilter
Create a filter for polling for new logs by passing into subscribe_getFilterChanges
as parameter
Parameters:
FilterParam
addrRange
:map[Address]Range
Query logs of the specified account address with given query range. Addresses and height ranges are filled in a map. At least one address must be specified.topics
:[][]Hash
Subscribed topics, see below example for usage.
Range
fromHeight
:uint64
Start height. 0 means starting from the first blocktoHeight
:uint64
End height. 0 means the latest block
# subscribe_uninstallFilter
Cancel event polling by removing specified filter
Parameters:
string
: filterId
Returns:
bool
result
# subscribe_getFilterChanges
Poll for new events. The content of return value depends on the specific subscription. If no event is generated since last poll, an empty array will be returned.
Parameters:
string
: filterId
Return in
subscribe_newSnapshotBlocksFilter
:subscription
:string
filterIdresult
:Array<SnapshotBlocksMsg>
hash
:Hash
The hash of snapshot blockheightStr
:string
The height of snapshot blockremoved
:bool
Whether the block was rolled back. New transaction will be marked asfalse
- Return in
subscribe_newAccountBlocksFilter
:subscription
:string
filterIdresult
:Array<AccountBlocksMsg>
hash
:Hash
The hash of account blockremoved
:bool
Whether the block was rolled back. New transaction will be marked asfalse
- Return in
subscribe_newAccountBlocksByAddrFilter
:subscription
:string
filterIdresult
:Array<AccountBlocksWithHeightMsg>
hash
:Hash
The hash of account blockheightStr
:string
The height of account blockremoved
:bool
Whether the block was rolled back. New transaction will be marked asfalse
- Return in
subscribe_newOnroadBlocksByAddrFilter
:subscription
:string
filterIdresult
:Array<OnroadMsg>
hash
:Hash
The hash of account blockclosed
:bool
Whether the transaction was receivedremoved
:bool
Whether the un-received transaction was rolled back
- Return in
subscribe_newLogsFilter
:subscription
:string
filterIdresult
:Array<LogsMsg>
accountBlockHash
:Hash
The hash of account blockaddr
:Address
Account addresslog
:VmLog
Logremoved
:bool
Whether the log was rolled back. New log will be marked asfalse
# subscribe_newSnapshotBlocks
Start listening for new snapshot blocks via callback
Returns:
string
Subscription id
Callback:
Object
subscription
:string
Subscription idresult
:Array<SnapshotBlocksMsg>
hash
:Hash
The hash of snapshot blockheight
:uint64
The height of snapshot blockremoved
:bool
Whether the block was rolled back. New transaction will be marked asfalse
# subscribe_newAccountBlocks
Start listening for new transactions via callback
Returns:
string
Subscription id
Callback:
Object
subscription
:string
Subscription idresult
:Array<AccountBlocksMsg>
hash
:Hash
The hash of account blockremoved
:bool
Whether the block was rolled back. New transaction will be marked asfalse
# subscribe_newAccountBlocksByAddr
Start listening for new transactions on specified account via callback
Parameters:
address
Account address
Returns:
string
Subscription id
Callback:
Object
subscription
:string
Subscription idresult
:Array<AccountBlocksWithHeightMsg>
hash
:Hash
The hash of account blockheightStr
:string
The height of account blockremoved
:bool
Whether the block was rolled back. New transaction will be marked asfalse
# subscribe_newOnroadBlocksByAddr
Start listening for un-received transactions on specified account via callback
Parameters:
address
Account address
Returns:
string
Subscription id
Callback:
Object
subscription
:string
Subscription idresult
:Array<OnroadMsg>
hash
:Hash
The hash of account blockclosed
:bool
Whether the transaction was receivedremoved
:bool
Whether the un-received transaction was rolled back
# subscribe_newLogs
Start listening for new logs via callback
Parameters:
FilterParam
addrRange
:map[Address]Range
Query logs of the specified account address with given query range. Addresses and height ranges are filled in a map. At least one address must be specified.topics
:[][]Hash
Subscribed topics, see example insubscribe_newLogsFilter
for usage.
Range
fromHeight
:uint64
Start height. 0 means starting from the first blocktoHeight
:uint64
End height. 0 means the latest block
Returns:
string
Subscription id
Callback:
Object
subscription
:string
Subscription idresult
:Array<LogsMsg>
accountBlockHash
:Hash
The hash of account blockaddr
:Address
Account addresslog
:VmLog
Logremoved
:bool
Whether the log was rolled back. New log will be marked asfalse
# subscribe_getLogs
Return historical logs
Parameters:
FilterParam
addrRange
:map[Address]Range
Query logs of the specified account address with given query range. Addresses and height ranges are filled in a map. At least one address must be specified.topics
:[][]Hash
Subscribed topics, see example insubscribe_newLogsFilter
for usage.
Range
fromHeight
:uint64
Start height. 0 means starting from the first blocktoHeight
:uint64
End height. 0 means the latest block
- Returns:
result
:Array<LogsMsg>
accountBlockHash
:Hash
The hash of account blockaddr
:Address
Account addresslog
:VmLog
Logremoved
:bool
Whether the log was rolled back. New log will be marked asfalse