ReplicaTransport

In the context of the Internet Computer blockchain, a Replica refers to the Internet Computer protocol processes running on a node.

To be able to connect remotely to the Internet Computer Canister IC4J implements ReplicaTransport interface over different Java HTTP Client libraries. Developers can choose specific implementations based on their Java application use cases.

ReplicaTransport interface currently supports 4 Internet Computer functions.

Calls in ReplicaTransport interface are asynchronous and return the CompletableFuture response type.

public interface ReplicaTransport {
    public CompletableFuture<byte[]> status();
    public CompletableFuture<byte[]> query(Principal canisterId, byte[] envelope);
    public CompletableFuture<byte[]> call(Principal canisterId, byte[] envelope, RequestId requestId);
    public CompletableFuture<byte[]> readState(Principal canisterId, byte[] envelope);
}

Apache HTTP 5 Client transport implementation

The Apache HTTP 5 library is a robust, stable Java implementation of the HTTP protocol. It allows developers to define advanced features like connection pooling.

The simplest way to create ReplicaTransport is to use ReplicaApacheHttpTransport to create the Method with the IC URL String as a parameter.

ReplicaTransport transport = 
ReplicaApacheHttpTransport.create("http://localhost:4943/");

For advanced use cases, for example, to create Java server type applications handling a large number of clients and canisters, additional parameters can be defined.

ReplicaTransport transport = 
ReplicaApacheHttpTransport.create("http://localhost:4943/", maxTotal, maxPerRoute,
 connectionTimeToLive, int timeout);

For even more complex scenarios ReplicaTransport can be created with the explicitly defined Apache HTTP Client connection manager AsyncClientConnectionManager.

ReplicaTransport transport = 
ReplicaApacheHttpTransport.create("http://localhost:4943/",asyncClientConnectionManager, int timeout);

OkHttp Client transport implementation

For Android development it is recommended to use the OkHttp Client Implementation.

OkHttp is an efficient HTTP & HTTP/2 client for Android and Java applications.

Use ReplicaOkHttpTransport to create the Method with the IC URL String as a parameter to create OkHttp ReplicaTransport

ReplicaTransport transport = 
ReplicaOkHttpTransport.create("http://localhost:4943/");

If needed, the Connection Timeout can be explicitly defined :

ReplicaTransport transport = 
ReplicaOkHttpTransport.create("http://localhost:4943/", timeout);

Java 11 HTTP Client transport implementation

From Java version 11 and higher, Oracle significantly improved functionality of Java default HTTP Client.

To make core libraries compatible with Java version 1.8, it is recommended that this version of transport is explicitly imported in the Graven or Maven build script.

implementation 'org.ic4j:ic4j-java11transport:0.7.0'

Use ReplicaJavaHttpTransport to create the Method with the IC URL String as a parameter to create Java 11 ReplicaTransport

ReplicaTransport transport = 
ReplicaJavaHttpTransport.create("http://localhost:4943/");

If needed the connection timeout can be defined explicitly.

ReplicaTransport transport = 
ReplicaJavaHttpTransport.create("http://localhost:4943/", timeout);

Last updated