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
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
{
"type": "Block",
"platform": "Fabric",
"instance_id": "csi-e2ehmfqasth-bcw7tzao2dzeo",
"network": "channel3",
"id": "block-282",
"name": "282",
"content": {
"number": 282,
"hash": "f7b42d07509c463f8e8cde0fcc8325b42e8bf7fd6a7a24dd207eed297fcd6358",
"create_time": 1581589504,
"previous_hash": "b8de08d49217c1d8f4ea398c8c61fe2a5bc46ec7bb6c95f44dfd29eaf6c272ab",
"transactions": [
"dc90b701e27afa5120ee003ef9d51b86a2ff9e1a9d8537f65387bde6d92c5cba"
]
}
}
Error codes
For more information, see Error codes.