[{"data":1,"prerenderedAt":813},["ShallowReactive",2],{"$flTJVCE3gofzIzwZ4qLk077_ogofZB1IbMgkvf3S2DHc":3},{"slug":4,"title":5,"description":6,"source":7,"document":8,"toc":796,"editUrl":808,"prev":809,"next":811},"techniques\u002Fgrpc","gRPC","Expose gRPC services on the same module model with ferrite-grpc.","---\ntitle: gRPC\ndescription: Expose gRPC services on the same module model with ferrite-grpc.\n---\n\n# gRPC\n\n`ferrite-grpc` brings tonic-based gRPC to the Ferrite module model. A service is a provider you register, and the gRPC server starts automatically during bootstrap.\n\n## Enable it\n\n```cargo\nfr add grpc\n```\n\n```rust\n#[module(imports = [GrpcModuleImpl])]\npub struct AppModule;\n```\n\n## Start it\n\n`GrpcModuleImpl` implements `OnApplicationBootstrap`, so the server starts when the app is ready. Configure the address at startup:\n\n```rust\n#[bootstrap]\nasync fn main() {\n    let app = Ferrite::create::\u003CAppModule>().await;\n\n    let grpc: Arc\u003CGrpcServer> = app.container.get::\u003CGrpcServer>();\n    tokio::spawn(async move {\n        grpc.start(\"0.0.0.0:50051\").await.expect(\"grpc server failed\");\n    });\n\n    app.listen(\"0.0.0.0:3000\").await?;\n}\n```\n\n## Registering services\n\nGenerated tonic services expose a wrapper constructor `new(impl Services)`. Register them with `submit_grpc_service!`:\n\n```rust\nuse ferrite_grpc::submit_grpc_service;\n\n\u002F\u002F Simple form: type implements components needed + Default-compatible\nsubmit_grpc_service!(GreeterService);\n\n\u002F\u002F Advanced form: (provider type, wrapper type) — the server downcasts the\n\u002F\u002F provider from the container and builds the tonic wrapper:\nsubmit_grpc_service!(GreeterService, GreeterServerWrapper);\n```\n\nThe second form produces:\n\n```rust\nlet typed: Arc\u003CGreeterService> = container.try_get_any(type_id)?.downcast().ok();\nsrv.add_service(\u003CGreeterServerWrapper>::new((*typed).clone()));\n```\n\n### Manual registration\n\n```rust\nlet grpc: Arc\u003CGrpcServer> = container.get::\u003CGrpcServer>();\ngrpc.add_service(GreeterServer::new(my_impl));\ngrpc.service_names()   \u002F\u002F inspect registered names\n```\n\n## `GrpcServer`\n\n| Method | Purpose |\n| --- | --- |\n| `add_service\u003CT>(&self, service: T)` | Register a generated service |\n| `build_server() -> Router` | Assemble the tonic router from inventory |\n| `start\u003CA: ToSocketAddrs>(&self, addr)` | Bind and serve |\n| `service_names() -> Vec\u003CString>` | Registered service names |\n\nThe inventory-driven `build_server` walks every `submit_grpc_service!` registration, resolving each service from the container. A `PlaceholderService` is always added so the router is never empty (it responds with `grpc-status: 12`, `NOT_IMPLEMENTED`).\n\n## Protobuf & codegen\n\n`ferrite-grpc` is wired to `tonic` \u002F `prost`:\n\n```toml\ntonic = { version = \"0.13\", features = [\"prost\", \"transport\", \"router\", \"server\"] }\nprost = \"0.14\"\nprost-types = \"0.14\"\n```\n\nGenerate stubs from `.proto` files with `tonic-build` as usual, then register the generated types.\n\n## Health & timeouts\n\n| Env | Default |\n| --- | --- |\n| `GRPC_HEALTH_TIMEOUT_MS` | `30000` |\n\nErrors: `GrpcError::{Transport(String), AlreadyExists(String), InvalidAddress(String)}`, with a `From\u003Ctonic::transport::Error>` conversion.\n\n## Example\n\nThe microservices examples (`micro-gateway`, `micro-sqlx-users`, etc.) show a gRPC fleet talking through an HTTP gateway.\n\n## Next steps\n\n- [Messaging (NATS & Kafka)](\u002Fdocs\u002Ftechniques\u002Fmessaging).\n- [WebSockets](\u002Fdocs\u002Ftechniques\u002Fwebsockets).",{"frontmatter":9,"meta":10,"nodes":44},{"title":5,"description":6},{"toc":11},{"title":12,"searchDepth":13,"depth":14,"links":15},"On This Page",2,3,[16,19,22,29,32,35,38,41],{"id":17,"depth":13,"text":18},"enable-it","Enable it",{"id":20,"depth":13,"text":21},"start-it","Start it",{"id":23,"depth":13,"text":24,"children":25},"registering-services","Registering services",[26],{"id":27,"depth":14,"text":28},"registering-services-manual-registration","Manual registration",{"id":30,"depth":13,"text":31},"grpcserver","GrpcServer",{"id":33,"depth":13,"text":34},"protobuf-codegen","Protobuf & codegen",{"id":36,"depth":13,"text":37},"health-timeouts","Health & timeouts",{"id":39,"depth":13,"text":40},"example","Example",{"id":42,"depth":13,"text":43},"next-steps","Next steps",[45,49,57,60,69,114,116,125,318,320,330,394,397,489,492,563,567,627,648,650,662,715,726,728,754,765,767,777,779],[46,47,5],"h1",{"id":48},"grpc",[50,51,52,56],"p",{},[53,54,55],"code",{},"ferrite-grpc"," brings tonic-based gRPC to the Ferrite module model. A service is a provider you register, and the gRPC server starts automatically during bootstrap.",[58,59,18],"h2",{"id":17},[61,62,65],"pre",{"language":63,"class":64},"cargo","shiki dark:github-dark",[53,66,68],{"class":67},"language-cargo","fr add grpc",[61,70,73],{"language":71,"class":72},"rust","shiki shiki-themes github-light github-dark dark:github-dark",[53,74,76,99,100],{"class":75},"language-rust",[77,78,81,85,89,92,96],"span",{"class":79,"style":80},"line","display: inline",[77,82,84],{"style":83},"color:#24292E;--shiki-dark:#E1E4E8","#[module(imports ",[77,86,88],{"style":87},"color:#D73A49;--shiki-dark:#F97583","=",[77,90,91],{"style":83}," [",[77,93,95],{"style":94},"color:#6F42C1;--shiki-dark:#B392F0","GrpcModuleImpl",[77,97,98],{"style":83},"])]","\n",[77,101,102,105,108,111],{"class":79,"style":80},[77,103,104],{"style":87},"pub",[77,106,107],{"style":87}," struct",[77,109,110],{"style":94}," AppModule",[77,112,113],{"style":83},";",[58,115,21],{"id":20},[50,117,118,120,121,124],{},[53,119,95],{}," implements ",[53,122,123],{},"OnApplicationBootstrap",", so the server starts when the app is ready. Configure the address at startup:",[61,126,127],{"language":71,"class":72},[53,128,129,99,134,99,148,99,183,99,185,99,230,99,251,99,284,99,289,99,291,99,313],{"class":75},[77,130,131],{"class":79,"style":80},[77,132,133],{"style":83},"#[bootstrap]",[77,135,136,139,142,145],{"class":79,"style":80},[77,137,138],{"style":87},"async",[77,140,141],{"style":87}," fn",[77,143,144],{"style":94}," main",[77,146,147],{"style":83},"() {",[77,149,150,153,156,158,161,164,167,169,172,175,178,181],{"class":79,"style":80},[77,151,152],{"style":87},"    let",[77,154,155],{"style":83}," app ",[77,157,88],{"style":87},[77,159,160],{"style":94}," Ferrite",[77,162,163],{"style":87},"::",[77,165,166],{"style":94},"create",[77,168,163],{"style":87},[77,170,171],{"style":83},"\u003C",[77,173,174],{"style":94},"AppModule",[77,176,177],{"style":83},">()",[77,179,180],{"style":87},".await",[77,182,113],{"style":83},[77,184],{"class":79,"style":80},[77,186,187,189,192,195,198,200,202,205,207,210,213,216,218,221,223,225,227],{"class":79,"style":80},[77,188,152],{"style":87},[77,190,191],{"style":83}," grpc",[77,193,194],{"style":87},":",[77,196,197],{"style":94}," Arc",[77,199,171],{"style":83},[77,201,31],{"style":94},[77,203,204],{"style":83},"> ",[77,206,88],{"style":87},[77,208,209],{"style":83}," app",[77,211,212],{"style":87},".",[77,214,215],{"style":83},"container",[77,217,212],{"style":87},[77,219,220],{"style":94},"get",[77,222,163],{"style":87},[77,224,171],{"style":83},[77,226,31],{"style":94},[77,228,229],{"style":83},">();",[77,231,232,235,237,240,243,245,248],{"class":79,"style":80},[77,233,234],{"style":94},"    tokio",[77,236,163],{"style":87},[77,238,239],{"style":94},"spawn",[77,241,242],{"style":83},"(",[77,244,138],{"style":87},[77,246,247],{"style":87}," move",[77,249,250],{"style":83}," {",[77,252,253,256,258,261,263,267,270,273,276,278,281],{"class":79,"style":80},[77,254,255],{"style":83},"        grpc",[77,257,212],{"style":87},[77,259,260],{"style":94},"start",[77,262,242],{"style":83},[77,264,266],{"style":265},"color:#032F62;--shiki-dark:#9ECBFF","\"0.0.0.0:50051\"",[77,268,269],{"style":83},")",[77,271,272],{"style":87},".await.",[77,274,275],{"style":94},"expect",[77,277,242],{"style":83},[77,279,280],{"style":265},"\"grpc server failed\"",[77,282,283],{"style":83},");",[77,285,286],{"class":79,"style":80},[77,287,288],{"style":83},"    });",[77,290],{"class":79,"style":80},[77,292,293,296,298,301,303,306,308,311],{"class":79,"style":80},[77,294,295],{"style":83},"    app",[77,297,212],{"style":87},[77,299,300],{"style":94},"listen",[77,302,242],{"style":83},[77,304,305],{"style":265},"\"0.0.0.0:3000\"",[77,307,269],{"style":83},[77,309,310],{"style":87},".await?",[77,312,113],{"style":83},[77,314,315],{"class":79,"style":80},[77,316,317],{"style":83},"}",[58,319,24],{"id":23},[50,321,322,323,326,327,194],{},"Generated tonic services expose a wrapper constructor ",[53,324,325],{},"new(impl Services)",". Register them with ",[53,328,329],{},"submit_grpc_service!",[61,331,332],{"language":71,"class":72},[53,333,334,99,347,99,349,99,355,99,366,99,368,99,373,99,378],{"class":75},[77,335,336,339,342,344],{"class":79,"style":80},[77,337,338],{"style":87},"use",[77,340,341],{"style":94}," ferrite_grpc",[77,343,163],{"style":87},[77,345,346],{"style":83},"submit_grpc_service;",[77,348],{"class":79,"style":80},[77,350,351],{"class":79,"style":80},[77,352,354],{"style":353},"color:#6A737D;--shiki-dark:#6A737D","\u002F\u002F Simple form: type implements components needed + Default-compatible",[77,356,357,359,361,364],{"class":79,"style":80},[77,358,329],{"style":94},[77,360,242],{"style":83},[77,362,363],{"style":94},"GreeterService",[77,365,283],{"style":83},[77,367],{"class":79,"style":80},[77,369,370],{"class":79,"style":80},[77,371,372],{"style":353},"\u002F\u002F Advanced form: (provider type, wrapper type) — the server downcasts the",[77,374,375],{"class":79,"style":80},[77,376,377],{"style":353},"\u002F\u002F provider from the container and builds the tonic wrapper:",[77,379,380,382,384,386,389,392],{"class":79,"style":80},[77,381,329],{"style":94},[77,383,242],{"style":83},[77,385,363],{"style":94},[77,387,388],{"style":83},", ",[77,390,391],{"style":94},"GreeterServerWrapper",[77,393,283],{"style":83},[50,395,396],{},"The second form produces:",[61,398,399],{"language":71,"class":72},[53,400,401,99,449],{"class":75},[77,402,403,406,409,411,413,415,417,419,421,424,426,429,432,435,438,441,443,446],{"class":79,"style":80},[77,404,405],{"style":87},"let",[77,407,408],{"style":83}," typed",[77,410,194],{"style":87},[77,412,197],{"style":94},[77,414,171],{"style":83},[77,416,363],{"style":94},[77,418,204],{"style":83},[77,420,88],{"style":87},[77,422,423],{"style":83}," container",[77,425,212],{"style":87},[77,427,428],{"style":94},"try_get_any",[77,430,431],{"style":83},"(type_id)",[77,433,434],{"style":87},"?.",[77,436,437],{"style":94},"downcast",[77,439,440],{"style":83},"()",[77,442,212],{"style":87},[77,444,445],{"style":94},"ok",[77,447,448],{"style":83},"();",[77,450,451,454,456,459,462,464,467,469,472,475,478,481,483,486],{"class":79,"style":80},[77,452,453],{"style":83},"srv",[77,455,212],{"style":87},[77,457,458],{"style":94},"add_service",[77,460,461],{"style":83},"(\u003C",[77,463,391],{"style":94},[77,465,466],{"style":83},">",[77,468,163],{"style":87},[77,470,471],{"style":94},"new",[77,473,474],{"style":83},"((",[77,476,477],{"style":87},"*",[77,479,480],{"style":83},"typed)",[77,482,212],{"style":87},[77,484,485],{"style":94},"clone",[77,487,488],{"style":83},"()));",[490,491,28],"h3",{"id":27},[61,493,494],{"language":71,"class":72},[53,495,496,99,528,99,548],{"class":75},[77,497,498,500,502,504,506,508,510,512,514,516,518,520,522,524,526],{"class":79,"style":80},[77,499,405],{"style":87},[77,501,191],{"style":83},[77,503,194],{"style":87},[77,505,197],{"style":94},[77,507,171],{"style":83},[77,509,31],{"style":94},[77,511,204],{"style":83},[77,513,88],{"style":87},[77,515,423],{"style":83},[77,517,212],{"style":87},[77,519,220],{"style":94},[77,521,163],{"style":87},[77,523,171],{"style":83},[77,525,31],{"style":94},[77,527,229],{"style":83},[77,529,530,532,534,536,538,541,543,545],{"class":79,"style":80},[77,531,48],{"style":83},[77,533,212],{"style":87},[77,535,458],{"style":94},[77,537,242],{"style":83},[77,539,540],{"style":94},"GreeterServer",[77,542,163],{"style":87},[77,544,471],{"style":94},[77,546,547],{"style":83},"(my_impl));",[77,549,550,552,554,557,560],{"class":79,"style":80},[77,551,48],{"style":83},[77,553,212],{"style":87},[77,555,556],{"style":94},"service_names",[77,558,559],{"style":83},"()   ",[77,561,562],{"style":353},"\u002F\u002F inspect registered names",[58,564,565],{"id":30},[53,566,31],{},[568,569,570,583],"table",{},[571,572,573],"thead",{},[574,575,576,580],"tr",{},[577,578,579],"th",{},"Method",[577,581,582],{},"Purpose",[584,585,586,597,607,617],"tbody",{},[574,587,588,594],{},[589,590,591],"td",{},[53,592,593],{},"add_service\u003CT>(&self, service: T)",[589,595,596],{},"Register a generated service",[574,598,599,604],{},[589,600,601],{},[53,602,603],{},"build_server() -> Router",[589,605,606],{},"Assemble the tonic router from inventory",[574,608,609,614],{},[589,610,611],{},[53,612,613],{},"start\u003CA: ToSocketAddrs>(&self, addr)",[589,615,616],{},"Bind and serve",[574,618,619,624],{},[589,620,621],{},[53,622,623],{},"service_names() -> Vec\u003CString>",[589,625,626],{},"Registered service names",[50,628,629,630,633,634,636,637,640,641,388,644,647],{},"The inventory-driven ",[53,631,632],{},"build_server"," walks every ",[53,635,329],{}," registration, resolving each service from the container. A ",[53,638,639],{},"PlaceholderService"," is always added so the router is never empty (it responds with ",[53,642,643],{},"grpc-status: 12",[53,645,646],{},"NOT_IMPLEMENTED",").",[58,649,34],{"id":33},[50,651,652,654,655,658,659,194],{},[53,653,55],{}," is wired to ",[53,656,657],{},"tonic"," \u002F ",[53,660,661],{},"prost",[61,663,665],{"language":664,"class":72},"toml",[53,666,668,99,700,99,708],{"class":667},"language-toml",[77,669,670,673,676,679,682,684,687,689,692,694,697],{"class":79,"style":80},[77,671,672],{"style":83},"tonic = { version = ",[77,674,675],{"style":265},"\"0.13\"",[77,677,678],{"style":83},", features = [",[77,680,681],{"style":265},"\"prost\"",[77,683,388],{"style":83},[77,685,686],{"style":265},"\"transport\"",[77,688,388],{"style":83},[77,690,691],{"style":265},"\"router\"",[77,693,388],{"style":83},[77,695,696],{"style":265},"\"server\"",[77,698,699],{"style":83},"] }",[77,701,702,705],{"class":79,"style":80},[77,703,704],{"style":83},"prost = ",[77,706,707],{"style":265},"\"0.14\"",[77,709,710,713],{"class":79,"style":80},[77,711,712],{"style":83},"prost-types = ",[77,714,707],{"style":265},[50,716,717,718,721,722,725],{},"Generate stubs from ",[53,719,720],{},".proto"," files with ",[53,723,724],{},"tonic-build"," as usual, then register the generated types.",[58,727,37],{"id":36},[568,729,730,740],{},[571,731,732],{},[574,733,734,737],{},[577,735,736],{},"Env",[577,738,739],{},"Default",[584,741,742],{},[574,743,744,749],{},[589,745,746],{},[53,747,748],{},"GRPC_HEALTH_TIMEOUT_MS",[589,750,751],{},[53,752,753],{},"30000",[50,755,756,757,760,761,764],{},"Errors: ",[53,758,759],{},"GrpcError::{Transport(String), AlreadyExists(String), InvalidAddress(String)}",", with a ",[53,762,763],{},"From\u003Ctonic::transport::Error>"," conversion.",[58,766,40],{"id":39},[50,768,769,770,388,773,776],{},"The microservices examples (",[53,771,772],{},"micro-gateway",[53,774,775],{},"micro-sqlx-users",", etc.) show a gRPC fleet talking through an HTTP gateway.",[58,778,43],{"id":42},[780,781,782,790],"ul",{},[783,784,785,212],"li",{},[786,787,789],"a",{"href":788},"\u002Fdocs\u002Ftechniques\u002Fmessaging","Messaging (NATS & Kafka)",[783,791,792,212],{},[786,793,795],{"href":794},"\u002Fdocs\u002Ftechniques\u002Fwebsockets","WebSockets",{"title":12,"searchDepth":13,"depth":14,"links":797},[798,799,800,803,804,805,806,807],{"id":17,"depth":13,"text":18},{"id":20,"depth":13,"text":21},{"id":23,"depth":13,"text":24,"children":801},[802],{"id":27,"depth":14,"text":28},{"id":30,"depth":13,"text":31},{"id":33,"depth":13,"text":34},{"id":36,"depth":13,"text":37},{"id":39,"depth":13,"text":40},{"id":42,"depth":13,"text":43},"https:\u002F\u002Fgithub.com\u002Fj4flmao\u002Fferrite_rs\u002Fedit\u002Fmain\u002Fdocs\u002Fcontent\u002Ftechniques\u002Fgrpc.md",{"title":795,"slug":810},"techniques\u002Fwebsockets",{"title":789,"slug":812},"techniques\u002Fmessaging",1789565439199]