sgcWebSockets now ships a native gRPC client for Delphi and C++Builder. The new TsgcGRPCClient component speaks gRPC over the library's own HTTP/2 transport, so there is no external gRPC runtime, no C library and no extra DLLs to deploy. It is part of the Enterprise edition and runs on Windows, macOS, Linux, iOS and Android.
gRPC is just Protocol Buffers messages framed over HTTP/2. sgcWebSockets already has a complete HTTP/2 stack, so the gRPC client builds straight on top of it: it frames your request bytes, applies the gRPC headers and timeouts, and parses the status and trailers back into typed results.
Built on the native HTTP/2 transport
A gRPC channel is an HTTP/2 connection. You create a TsgcHTTP2Client, point it at the host and port, then assign it to the Client property of the gRPC component. TLS is handled by the HTTP/2 client through OpenSSL or Windows SChannel.
uses
sgcHTTP2, sgcGRPC_Client, sgcGRPC_Classes, sgcGRPC_Types;
var
HTTP2: TsgcHTTP2Client;
GRPC: TsgcGRPCClient;
oResponse: TsgcGRPCResponse;
begin
HTTP2 := TsgcHTTP2Client.Create(nil);
HTTP2.Host := 'grpc.example.com';
HTTP2.Port := 443;
HTTP2.TLS := True;
GRPC := TsgcGRPCClient.Create(nil);
GRPC.Client := HTTP2;
// metadata is sent on every call (auth, tracing...)
GRPC.DefaultMetadata.Add('authorization', 'Bearer eyJ...');
// unary call: the request is your serialized protobuf message as TBytes
oResponse := GRPC.Call('helloworld.Greeter', 'SayHello', RequestBytes);
if oResponse.StatusCode = grpcOK then
Memo1.Text := oResponse.DataString
else
ShowMessage('gRPC error: ' + oResponse.StatusMessage);
end;
All four call types
The component supports every gRPC interaction pattern, synchronous or asynchronous:
- Unary.
Callblocks and returns aTsgcGRPCResponse;CallAsyncreturns at once and firesOnGRPCResponse. - Server streaming.
ServerStreamingCallsends one request and receives a stream of messages, each raisingOnGRPCStreamMessage, withOnGRPCStreamEndat the close. - Client streaming.
OpenClientStream, thenSendStreamMessagefor each message, thenCloseClientStreamto read the single reply. - Bidirectional streaming.
OpenBidiStream,SendBidiMessageandCloseBidiStreamrun a full-duplex exchange over one HTTP/2 stream.
A response carries everything the call returned: StatusCode, StatusMessage, the raw Data bytes (or DataString) and the Trailers.
Metadata, deadlines and channel options
Custom headers travel through DefaultMetadata for the whole channel, or per call. A per-call timeout is translated to the standard grpc-timeout header, and CancelCall aborts an in-flight stream. The channel itself is tuned through ChannelOptions.
// gzip compression and channel limits
GRPC.ChannelOptions.Compression := grpcGzip;
GRPC.ChannelOptions.MaxMessageSize := 16 * 1024 * 1024;
// abort a long-running stream
GRPC.CancelCall(StreamId);
Interceptors, retry and observability
An Interceptors chain wraps every call, so you can add logging, auth refresh or tracing in one place. RetryPolicy retries on the status codes you choose, ServiceConfig applies per-method policy, and MetricsCollector records call counters and latencies.
GRPC.RetryPolicy.MaxAttempts := 4;
GRPC.RetryPolicy.RetryableStatusCodes :=
GRPC.RetryPolicy.RetryableStatusCodes + [grpcUNAVAILABLE];
Bring your own Protocol Buffers
The client takes and returns raw message bytes (TBytes), so it does not lock you into any single Protocol Buffers library. Encode your request with whatever protobuf tooling you already use, pass the bytes to Call, and decode the response bytes the same way. The component handles the gRPC length-prefix framing, compression flag, status codes and trailers for you.
Google Cloud gRPC APIs included
On top of the generic client, sgcWebSockets ships typed clients for the Google Cloud gRPC services: Pub/Sub, Speech-to-Text, Translation, Vision, Natural Language, Cloud Storage, BigQuery and Vertex AI. They handle the service-account authentication and the request and response messages, so you call high-level methods instead of building protobufs by hand.
Availability
The gRPC client is available now in the sgcWebSockets Enterprise edition for Delphi and C++Builder. A ready-to-run sample is included in Demos\21.GRPC\01.GRPC_Client, and the full property, method and event reference lives on the gRPC Client product page.
Questions, feedback or help getting started? Get in touch. You will get a reply from the people who wrote the code.
