All Products
Search
Document Center

Blockchain as a Service:Subscribe to Events

Last Updated:Mar 25, 2020

This topic describes how to use WebSocket to receive events that occurred on a blockchain network.

API

GET /api/v1/networks/{network}/events/subscribe

URL path parameter Description
network The name of the channel.
access_token The access token used to access the REST API.
source The subscribed event type. The options include Tx, Config, Contract, and Block. If you subscribe to multiple types of events, separate the types with commas (,). “*“ indicates that all types of events are subscribed.

Message content

Parameter Type Description
id String The ID of an event, which is unique in the channel.
name String The name of an event.
type EventType The type of an event. The options include Tx, Config, Contract, and Block.
Tx: transaction generation
Config: system configuration changes of a blockchain
Contract: smart contract setting events in transactions
Block: block generation
network String The name of the channel.
instance_id String The instance ID of the subscribed event, which is unique in an organization.
content Object When EventType is set to Tx or Config, the value is in Transaction structure.
When EventType is set to Contract, the value is of String type, and the event content is Base64-coded.
When EventType is set to Block, the value is in Block structure.

Transaction

Parameter Type Description
id String The ID of a transaction. The value is empty when EventType is set to Config.
state String The state of a transaction. “VALID” indicates that the transaction is legal. Other values indicate that the transaction is illegal.
from String The transaction initiator, which is in the format of <Organization MSP>. <Username.>
to String The target chaincode called by the transaction. The value is empty when EventType is set to Config.
input String The chaincode calling parameter after JSON coding. When EventType is set to Config, the value is JSON-coded common.ConfigUpdate.
events List<Event> The blockchain event list generated by the transaction.
data Object Detailed content of the transaction. The data structure is common.Payload.

Block

Parameter Type Description
number Integer Height of a block.
hash String The hash value of the current block after Hex coding.
previous_hash String The hash value of the last block after Hex coding.
create_time Integer The creation time of the current block, which is a UNIX timestamp representing the number of milliseconds that have elapsed since the epoch time January 1, 1970, 00:00:00 UTC.
transactions List<String> The transaction ID list of the current block.

Examples

Sample request

  1. wscat -c "ws://your.gateway.endpoint/api/v1/networks/channel3/events/subscribe?access_token=<Your URL Encoded Access Token>&source=%2A"

Sample of normal message pushing

  1. {
  2. "type": "Block",
  3. "platform": "Fabric",
  4. "instance_id": "csi-e2ehmfqasth-bcw7tzao2dzeo",
  5. "network": "channel3",
  6. "id": "block-282",
  7. "name": "282",
  8. "content": {
  9. "number": 282,
  10. "hash": "f7b42d07509c463f8e8cde0fcc8325b42e8bf7fd6a7a24dd207eed297fcd6358",
  11. "create_time": 1581589504,
  12. "previous_hash": "b8de08d49217c1d8f4ea398c8c61fe2a5bc46ec7bb6c95f44dfd29eaf6c272ab",
  13. "transactions": [
  14. "dc90b701e27afa5120ee003ef9d51b86a2ff9e1a9d8537f65387bde6d92c5cba"
  15. ]
  16. }
  17. }

Error codes

For more information, see Error codes.