Topic Reader Class

The TopicReader class is a core component of the resolver package. It allows to implement custom logic for reading messages from the Hedera network topic.

Overview

The TopicReader class provides a flexible interface for reading messages from the Hedera network topic. It allows to implement custom logic for reading messages from the Hedera network topic. Hedera DID SDK provides most common implementations of the TopicReader class. You can use them as is or extend them to implement custom logic. You can also implement your own TopicReader implementation based on your specific requirements.

Benefits of using the TopicReader class:

  • Flexible interface for reading messages from the Hedera network topic.

  • Implement custom logic for reading messages from the Hedera network topic.

  • Use Hedera DID SDK provided implementations of the TopicReader class.

  • Implement your own TopicReader implementation based on your specific requirements.

Use cases:

  • Read messages from the Hedera network topic using gRPC client.

  • Read messages from the Hedera network topic using REST API.

  • Read messages with additional caching capabilities.

  • Read messages for testing or development purposes.

Implementations

Hedera DID SDK provides the following implementations of the TopicReader class:

  • TopicReaderHederaClient - Read messages from the Hedera network topic using gRPC client (default implementation).

  • TopicReaderHederaRestApi - Read messages from the Hedera network topic using Mirror Node REST API (browser friendly).

TopicReaderHederaClient

The TopicReaderHederaClient class is a default implementation of the TopicReader class. It uses the Hedera gRPC client to read messages from the Hedera network topic. This implementation cannot be used in the browser environment.

import { TopicReaderHederaClient } from '@swiss-digital-assets-institute/resolver';

const topicReader = new TopicReaderHederaClient();
const messages = await topicReader.fetchFrom('0.0.5217215', 'testnet', {
  from: 0, // Read messages from the start of the topic
  to: 1000, // Read messages to 1000 seconds from the start of the topic
});

console.log(messages);
import { TopicReaderHederaClient } from '@swiss-digital-assets-institute/resolver';

const topicReader = new TopicReaderHederaClient();
const messages = await topicReader.fetchAllToDate('0.0.5217215', 'testnet');

console.log(messages);

TopicReaderHederaRestApi

The TopicReaderHederaRestApi class is a implementation of the TopicReader class that uses the Mirror Node REST API to read messages from the Hedera network topic.

import { TopicReaderHederaRestApi } from '@swiss-digital-assets-institute/resolver';

const topicReader = new TopicReaderHederaRestApi();
const messages = await topicReader.fetchFrom('0.0.5217215', 'testnet', {
  from: 0, // Read messages from the start of the topic
  to: 1000, // Read messages to 1000 seconds from the start of the topic
});
console.log(messages);
import { TopicReaderHederaRestApi } from '@swiss-digital-assets-institute/resolver';

const topicReader = new TopicReaderHederaRestApi();
const messages = await topicReader.fetchAllToDate('0.0.5217215', 'testnet');

console.log(messages);

Using Custom Rest API

By default, the TopicReaderHederaRestApi class uses the public Mirror Node REST API. You can use your own REST API endpoint by passing it to the constructor of the TopicReaderHederaRestApi class as a map of network names to REST API endpoints.

import { TopicReaderHederaRestApi } from '@swiss-digital-assets-institute/resolver';

const topicReader = new TopicReaderHederaRestApi({
  mainnet: 'https://mainnet.example.com',
  testnet: 'https://testnet.example.com',
  previewnet: 'https://previewnet.example.com',
  'local-node': 'http://local.example.com',
});

Make sure that your API is compliant with the Hedera Mirror Node REST API specification.