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.

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();
}
}
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?: EventsOptions): Promise<CloseableAsyncIterable<BlockAndPrivateData>>;
    getBlockEvents(options?: EventsOptions): Promise<CloseableAsyncIterable<Block>>;
    getChaincodeEvents(chaincodeName: string, options?: EventsOptions): Promise<CloseableAsyncIterable<ChaincodeEvent>>;
    getContract(chaincodeName: string, contractName?: string): Contract;
    getFilteredBlockEvents(options?: EventsOptions): Promise<CloseableAsyncIterable<FilteredBlock>>;
    getName(): string;
    newBlockAndPrivateDataEventsRequest(options?: EventsOptions): BlockAndPrivateDataEventsRequest;
    newBlockEventsRequest(options?: EventsOptions): BlockEventsRequest;
    newChaincodeEventsRequest(chaincodeName: string, options?: EventsOptions): ChaincodeEventsRequest;
    newFilteredBlockEventsRequest(options?: EventsOptions): 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.

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

    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.

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

    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.

    • Optionaloptions: EventsOptions

      Event listening options.

    Returns Promise<CloseableAsyncIterable<ChaincodeEvent>>

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

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

    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.

    • OptionalcontractName: 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.

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

    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