Streams real-time blockchain events from a blockchain network over a persistent WebSocket connection.
Request syntax
GET /api/v1/networks/{network}/events/subscribeRequest parameters
| Parameter | Description |
|---|---|
network | The name of the channel. |
access_token | The access token used to authenticate REST API requests. |
source | The event types to subscribe to. Valid values: Tx, Config, Contract, Block. To subscribe to multiple types, separate them with commas (,). Use * to subscribe to all event types. |
Message structure
Each message pushed over the WebSocket connection contains the following fields.
| Parameter | Type | Description |
|---|---|---|
id | String | The event ID, unique within the channel. |
name | String | The event name. |
type | EventType | The event type. Valid values: Tx (transaction creation), Config (blockchain system configuration change), Contract (smart contract event set in a transaction), Block (block creation). |
network | String | The name of the channel. |
instance_id | String | The instance ID of the subscribed event, unique within an organization. |
content | Object | The event payload. The structure depends on the event type: when type is Tx or Config, the value follows the Transaction structure; when type is Contract, the value is a Base64-encoded string; when type is Block, the value follows the Block structure. |
Transaction structure
Applies when type is Tx or Config.
| Parameter | Type | Description |
|---|---|---|
id | String | The transaction ID. Empty when type is Config. |
state | String | The transaction state. VALID indicates a valid transaction. Any other value indicates an invalid transaction. |
from | String | The transaction initiator, in the format <Organization MSP>.<Username>, where MSP is the Membership Service Provider (MSP) identifier. |
to | String | The target chaincode called by the transaction. Empty when type is Config. |
input | String | The JSON-encoded chaincode input parameters. When type is Config, this field contains the JSON-encoded common.ConfigUpdate value. |
events | List\<Event\> | The blockchain events generated by the transaction. |
data | Object | The full transaction payload. The data structure is common.Payload. |
Block structure
Applies when type is Block.
| Parameter | Type | Description |
|---|---|---|
number | Integer | The block height. |
hash | String | The hash of the current block, hex-encoded. |
previous_hash | String | The hash of the previous block, hex-encoded. |
create_time | Integer | The block creation time as a UNIX timestamp, representing the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC. |
transactions | List\<String\> | The list of transaction IDs included in the block. |
Examples
Connect to the event stream
wscat -c "ws://your.gateway.endpoint/api/v1/networks/channel3/events/subscribe?access_token=<Your URL Encoded Access Token>&source=%2A"Sample block event message
{
"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.