A Network represents the set of peers in a Fabric network. Applications should get a Network instance using the gateway's getNetwork method.
The Network object provides the ability for applications to:
- Obtain a specific smart contract deployed to the network using getContract, in order to submit and evaluate transactions for that smart contract.
- Listen to new block events and replay previous block events using addBlockListener.
Methods
-
<async> addBlockListener(listener [, options])
-
Add a listener to receive block events for this network. Blocks will be received in order and without duplication. The default is to listen for full block events from the current block position.
Parameters:
Name Type Argument Description listener
module:fabric-network.BlockListener A block listener callback function. options
module:fabric-network.ListenerOptions <optional>
Listener options. Returns:
The added listener.- Type
- Promise.<module:fabric-network.BlockListener>
Example
const listener: BlockListener = async (event) => { // Handle block event // Listener may remove itself if desired if (event.blockNumber.equals(endBlock)) { network.removeBlockListener(listener); } } const options: ListenerOptions = { startBlock: 1 }; await network.addBlockListener(listener, options);
-
addCommitListener(listener, peers, transactionId)
-
Add a listener to receive transaction commit and peer disconnect events for a set of peers. This is typically used only within the implementation of a custom transaction commit event handler.
Parameters:
Name Type Description listener
module:fabric-network.CommitListener A transaction commit listener callback function. peers
Array.<Endorser> The peers from which to receive events. transactionId
string A transaction ID. Returns:
The added listener.Example
const listener: CommitListener = (error, event) => { if (error) { // Handle peer communication error } else { // Handle transaction commit event } } const peers = network.channel.getEndorsers(); await network.addCommitListener(listener, peers, transactionId);
-
getChannel()
-
Get the underlying channel object representation of this network.
Returns:
A channel.- Type
- Channel
-
getContract(chaincodeId [, name] [, collections])
-
Get an instance of a contract (chaincode) on the current network.
Parameters:
Name Type Argument Description chaincodeId
string the chaincode identifier. name
string <optional>
the name of the contract. collections
Array.<string> <optional>
the names of collections defined for this chaincode. Returns:
the contract. -
getGateway()
-
Get the owning Gateway connection.
Returns:
A Gateway. -
removeBlockListener(listener)
-
Remove a previously added block listener.
Parameters:
Name Type Description listener
module:fabric-network.BlockListener A block listener callback function. -
removeCommitListener(listener)
-
Remove a previously added transaction commit listener.
Parameters:
Name Type Description listener
module:fabric-network.CommitListener A transaction commit listener callback function.