Network represents a network of nodes that are members of a specific Fabric channel. The Network can be used to access deployed smart contracts, and to listen for events emitted when blocks are committed to the ledger. Network instances are obtained from a Gateway using the Gateway.getNetwork method.

To safely handle connection errors during eventing, it is recommended to use a checkpointer to track eventing progress. This allows eventing to be resumed with no loss or duplication of events.

Example: Chaincode events

const checkpointer = checkpointers.inMemory();

while (true) {
const events = await network.getChaincodeEvents(chaincodeName, {
checkpoint: checkpointer,
startBlock: BigInt(101), // Ignored if the checkpointer has checkpoint state
});
try {
for await (const event of events) {
// Process then checkpoint event
await checkpointer.checkpointChaincodeEvent(event)
}
} catch (err: unknown) {
// Connection error
} finally {
events.close();
}
}

Example: Block events

const checkpointer = checkpointers.inMemory();

while (true) {
const events = await network.getBlockEvents({
checkpoint: checkpointer,
startBlock: BigInt(101), // Ignored if the checkpointer has checkpoint state
});
try {
for await (const event of events) {
// Process then checkpoint block
await checkpointer.checkpointBlock(event.getHeader().getNumber())
}
} catch (err: unknown) {
// Connection error
} finally {
events.close();
}
}
interface Network {
    getBlockAndPrivateDataEvents(options?): Promise<CloseableAsyncIterable<BlockAndPrivateData>>;
    getBlockEvents(options?): Promise<CloseableAsyncIterable<Block>>;
    getChaincodeEvents(chaincodeName, options?): Promise<CloseableAsyncIterable<ChaincodeEvent>>;
    getContract(chaincodeName, contractName?): Contract;
    getFilteredBlockEvents(options?): Promise<CloseableAsyncIterable<FilteredBlock>>;
    getName(): string;
    newBlockAndPrivateDataEventsRequest(options?): BlockAndPrivateDataEventsRequest;
    newBlockEventsRequest(options?): BlockEventsRequest;
    newChaincodeEventsRequest(chaincodeName, options?): ChaincodeEventsRequest;
    newFilteredBlockEventsRequest(options?): FilteredBlockEventsRequest;
}

Methods

  • Get block and private data events.

    Parameters

    Returns Promise<CloseableAsyncIterable<BlockAndPrivateData>>

    Blocks and private data protocol buffer messages. The iterator should be closed after use to complete the eventing session.

    Throws

    GatewayError Thrown by the iterator if the gRPC service invocation fails.

    Example

    const events = await network.getBlockAndPrivateEventsData({ startBlock: BigInt(101) });
    try {
    for await (const event of events) {
    // Process block and private data event
    }
    } finally {
    events.close();
    }
  • Get block events.

    Parameters

    Returns Promise<CloseableAsyncIterable<Block>>

    Block protocol buffer messages. The iterator should be closed after use to complete the eventing session.

    Throws

    GatewayError Thrown by the iterator if the gRPC service invocation fails.

    Example

    const blocks = await network.getBlockEvents({ startBlock: BigInt(101) });
    try {
    for async (const block of blocks) {
    // Process block
    }
    } finally {
    blocks.close();
    }
  • Get chaincode events emitted by transaction functions of a specific chaincode.

    Parameters

    • chaincodeName: string

      A chaincode name.

    • Optional options: EventsOptions

      Event listening options.

    Returns Promise<CloseableAsyncIterable<ChaincodeEvent>>

    The iterator should be closed after use to complete the eventing session.

    Throws

    GatewayError Thrown by the iterator if the gRPC service invocation fails.

    Example

    const events = await network.getChaincodeEvents(chaincodeName, { startBlock: BigInt(101) });
    try {
    for await (const event of events) {
    // Process event
    }
    } finally {
    events.close();
    }
  • Get a smart contract within the named chaincode. If no contract name is supplied, this is the default smart contract for the named chaincode.

    Parameters

    • chaincodeName: string

      Chaincode name.

    • Optional contractName: string

      Smart contract name.

    Returns Contract

  • Get filtered block events.

    Parameters

    Returns Promise<CloseableAsyncIterable<FilteredBlock>>

    Filtered block protocol buffer messages. The iterator should be closed after use to complete the eventing session.

    Throws

    GatewayError Thrown by the iterator if the gRPC service invocation fails.

    Example

    const blocks = await network.getFilteredBlockEvents({ startBlock: BigInt(101) });
    try {
    for async (const block of blocks) {
    // Process block
    }
    } finally {
    blocks.close();
    }
  • Get the name of the Fabric channel this network represents.

    Returns string