Examples
This guide walks you through gRPCServer.jl examples in progressive order, from basic concepts to advanced patterns.
Learning Path
| Example | RPC Pattern | Concepts | Directory |
|---|---|---|---|
| Hello World | Unary | Service definition, basic server | examples/01_hello_world/ |
| Hello Stream | Server Streaming | Multiple responses, cancellation | examples/02_hello_stream/ |
| Sum Numbers | Client Streaming | Multiple requests, aggregation | examples/03_sum_numbers/ |
| Chat | Bidirectional | Real-time messaging | examples/04_chat/ |
| Calculator | Unary (multi-method) | Error handling, multiple methods | examples/05_calculator/ |
| Advanced Topics | All patterns | Interceptors, TLS, compression | (documentation only) |
Getting Started
Prerequisites
Install gRPCServer.jl:
using Pkg Pkg.add("gRPCServer")Install grpcurl for testing:
# macOS brew install grpcurl # Linux (download binary from releases) # Windows (download binary from releases)
Running Examples
Each example directory contains:
*.proto- Protocol buffer service definitionserver.jl- Julia server implementationgenerated/- Auto-generated Julia typesREADME.md- Detailed usage instructions
To run any example:
cd examples/<example_name>
julia --project=../.. server.jlExample Workflow
- Start simple: Begin with
01_hello_worldto understand basic unary RPC - Server streaming: Move to
02_hello_streamto learn single request → multiple responses - Client streaming: Try
03_sum_numbersfor multiple requests → single response - Bidirectional: Explore
04_chatfor simultaneous bidirectional streaming - Multi-method: See
05_calculatorfor multiple methods with error handling - Production: Read advanced documentation for interceptors, TLS, compression
Quick Reference
Unary RPC (01helloworld)
Single request, single response:
function handler(ctx::ServerContext, request::RequestType)::ResponseType
return ResponseType(...)
endServer Streaming (02hellostream)
Single request, multiple responses:
function handler(ctx::ServerContext, request::RequestType, stream::ServerStream{ResponseType})::Nothing
for item in items
send!(stream, ResponseType(...))
end
return nothing
endClient Streaming (03sumnumbers)
Multiple requests, single response:
function handler(ctx::ServerContext, stream::ClientStream{RequestType})
for request in stream
# Process each request
end
return ResponseType(...)
endBidirectional Streaming (04_chat)
Multiple requests and responses:
function handler(ctx::ServerContext, stream::BidiStream{RequestType, ResponseType})
for request in stream
send!(stream, ResponseType(...))
end
close!(stream)
return nothing
endError Handling (05_calculator)
Return appropriate gRPC status codes:
if invalid_input
throw(GRPCError(StatusCode.INVALID_ARGUMENT, "Error message"))
endPort Assignments
Each example uses a different port:
| Example | Port |
|---|---|
| 01helloworld | 50051 |
| 02hellostream | 50051 |
| 03sumnumbers | 50053 |
| 04_chat | 50054 |
| 05_calculator | 50052 |
Next Steps
After completing the examples, explore:
- Quick Start - Comprehensive setup guide
- API Reference - Detailed API documentation