import ballerina/grpc;
import ballerina/log;
service HelloWorld on new grpc:Listener(9090) {    resource function hello(grpc:Caller caller, string name) {
        log:printInfo("Server received hello from " + name);
        string message = "Hello " + name;
        grpc:Error? result = caller->send(message);
        if (result is grpc:Error) {
            log:printError("Error from Connector: " + result.message());
        }
        result = caller->complete();
        if (result is grpc:Error) {
            log:printError("Error from Connector: " + result.message());
        }
    }
}
import ballerina/grpc;
import ballerina/io;int total = 0;
public function main() {
    HelloWorldClient helloWorldEp = new ("http://localhost:9090");
    grpc:Error? result = helloWorldEp->hello("WSO2", HelloWorldMessageListener);    if (result is grpc:Error) {
        io:println("Error from Connector: " + result.message());
    } else {
        io:println("Connected successfully");
    }    while (total == 0) {}
    io:println("Client got response successfully.");
}
service HelloWorldMessageListener = service {
    resource function onMessage(string message) {
        io:println("Response received from server: " + message);
    }
    resource function onError(error err) {
        io:println("Error reported from server: " + err.message());
    }
    resource function onComplete() {
        io:println("Server Complete Sending Response.");
        total = 1;
    }
};

Unary Non-Blocking

The gRPC Server Connector is used to expose gRPC services over HTTP/2. This sample demonstrates how the gRPC unary service interacts with the gRPC non-blocking client.

For more information on the underlying module, see the GRPC module.

syntax = "proto3";
package service;
import "google/protobuf/wrappers.proto";
service HelloWorld {
	rpc hello (google.protobuf.StringValue)
			returns (google.protobuf.StringValue);
}

This is the service definition for the unary blocking/unblocking scenario.

# Create new Protocol Buffers definition file `grpc_unary_non_blocking.proto` and add service definition.
# Run the command below in Ballerina tools distribution for stub generation.
ballerina grpc --input grpc_unary_non_blocking.proto  --output stubs
# Once you run the command, `grpc_unary_non_blocking_pb.bal` file is generated inside stubs directory.
# Please refer example `Proto To Ballerina` to get information on how to use Ballerina Protocol Buffers tool.
import ballerina/grpc;
import ballerina/log;

This is the server implementation for the unary blocking/unblocking scenario.

service HelloWorld on new grpc:Listener(9090) {

Bind the service to the port.

    resource function hello(grpc:Caller caller, string name) {
        log:printInfo("Server received hello from " + name);
        string message = "Hello " + name;
        grpc:Error? result = caller->send(message);
        if (result is grpc:Error) {
            log:printError("Error from Connector: " + result.message());
        }

Send a response message to the caller.

        result = caller->complete();
        if (result is grpc:Error) {
            log:printError("Error from Connector: " + result.message());
        }
    }
}

Send the completed notification to the caller.

# Create a Ballerina project and a module inside it.
# Copy generated stub file `grpc_unary_non_blocking_pb.bal` to the module.
# For example, if you create a module named `service`, copy the stub file to the `service` module.
# Add new Ballerina file `grpc_unary_non_blocking.bal` inside the `service` module and add service implementation.
# Execute the command below to build the 'service' module.
ballerina build service
# Run the service using the command below.
ballerina run target/bin/service.jar
import ballerina/grpc;
import ballerina/io;

This is the client implementation for the unary non blocking scenario.

int total = 0;
public function main() {
    HelloWorldClient helloWorldEp = new ("http://localhost:9090");

Client endpoint configuration.

    grpc:Error? result = helloWorldEp->hello("WSO2", HelloWorldMessageListener);

Execute the unary non-blocking call that registers the server message listener.

    if (result is grpc:Error) {
        io:println("Error from Connector: " + result.message());
    } else {
        io:println("Connected successfully");
    }
    while (total == 0) {}
    io:println("Client got response successfully.");
}
service HelloWorldMessageListener = service {

Server Message Listener.

    resource function onMessage(string message) {
        io:println("Response received from server: " + message);
    }

The resource registered to receive server messages.

    resource function onError(error err) {
        io:println("Error reported from server: " + err.message());
    }

The resource registered to receive server error messages.

    resource function onComplete() {
        io:println("Server Complete Sending Response.");
        total = 1;
    }
};

The resource registered to receive server completed messages.

# Create a Ballerina project and a module inside it.
# Copy generated stub file `grpc_unary_non_blocking_pb.bal` to the module.
# For example, if you create a module named `client`, copy the stub file to the `client` module.
# Add new Ballerina file `grpc_unary_non_blocking_client.bal` inside the `client` module and add client implementation.
# Execute the command below to build the 'client' module.
ballerina build client
# Run the client using the command below.
ballerina run target/bin/client.jar