import ballerina/grpc;
import ballerina/log;service HelloWorld on new grpc:Listener(9090) {    resource function lotsOfReplies(grpc:Caller caller, string name) {
        log:printInfo("Server received hello from " + name);
        string[] greets = ["Hi", "Hey", "GM"];
        foreach string greet in greets {
            string msg = greet + " " + name;
            grpc:Error? err = caller->send(msg);
            if (err is grpc:Error) {
                log:printError("Error from Connector: " + err.message());
            } else {
                log:printInfo("Send reply: " + msg);
            }
        }
        grpc:Error? result = caller->complete();
        if (result is grpc:Error) {
            log:printError("Error in sending completed notification to caller",
                err = result);
        }
    }
}
import ballerina/grpc;
import ballerina/io;int total = 0;
public function main() {
    HelloWorldClient helloWorldEp = new ("http://localhost:9090");
    grpc:Error? result = helloWorldEp->lotsOfReplies("Sam",
                                                    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 from Connector: " + err.message());
    }
    resource function onComplete() {
        total = 1;
        io:println("Server Complete Sending Responses.");
    }
};

Server Streaming

The gRPC Server Connector is used to expose gRPC services over HTTP/2. This sample includes a gRPC server streaming service and a non-blocking client. The client sends a request to the server and gets a stream to read the messages until all the messages are read.

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

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

This is the service definition for the server streaming scenario.

# Create new Protocol Buffers definition file `grpc_server_streaming.proto` and add service definition.
# Run the command below in Ballerina tools distribution for stub generation.
ballerina grpc --input grpc_server_streaming.proto  --output stubs
# Once you run the command, `grpc_server_streaming_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 server streaming scenario.

service HelloWorld on new grpc:Listener(9090) {
    resource function lotsOfReplies(grpc:Caller caller, string name) {
        log:printInfo("Server received hello from " + name);
        string[] greets = ["Hi", "Hey", "GM"];
        foreach string greet in greets {
            string msg = greet + " " + name;
            grpc:Error? err = caller->send(msg);
            if (err is grpc:Error) {
                log:printError("Error from Connector: " + err.message());
            } else {
                log:printInfo("Send reply: " + msg);
            }
        }

Send multiple messages to the caller.

        grpc:Error? result = caller->complete();
        if (result is grpc:Error) {
            log:printError("Error in sending completed notification to caller",
                err = result);
        }
    }
}

Once all the messages are sent, the server notifies the caller with a complete message.

# Create a Ballerina project and a module inside it.
# Copy generated stub file `grpc_server_streaming_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_server_streaming.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 server streaming scenario.

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

Client endpoint configuration.

    grpc:Error? result = helloWorldEp->lotsOfReplies("Sam",
                                                    HelloWorldMessageListener);
    if (result is grpc:Error) {
        io:println("Error from Connector: " + result.message());
    } else {
        io:println("Connected successfully");
    }

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

    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 from Connector: " + err.message());
    }

The resource registered to receive server error messages

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

The resource registered to receive server completed messages.

# Create a Ballerina project and a module inside it.
# Copy generated stub file `grpc_server_streaming_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_server_streaming_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