Skip to main content

gRPC transcoding and WebSocket Routes through the gateway

Call a gRPC service with plain JSON through grpc-transcode, with the proto kept in the console, and let a Route upgrade to WebSocket, both measured live.

By Akif F. Güven Updated Gateway

What you will have at the end

Two Routes for two protocols the gateway carries besides plain HTTP. hello-grpc takes a JSON body or a query string, turns it into a gRPC call to the hello service of the lab and turns the reply back into JSON, with the service’s proto definition kept as an object in the console. ws-echo lets a client upgrade its connection to WebSocket and exchange messages with a backend through the gateway, where the same request without the switch is a plain HTTP request the backend refuses. Both are request records in Traffic Explorer, and the proto, the Upstream and the Routes are in the audit log.

Prerequisites

  • A gRPC service. The lab runs grpcbin, which answers hello.HelloService/SayHello on port 9000 without TLS; a reply is hello followed by the greeting sent.
  • A backend that speaks WebSocket. The lab’s echo service answers /websocket/echo by sending every message back; it is the Upstream httpbin-backend of the other articles.
  • Traffic logging turned on under Traffic Explorer › Settings, and a role that may manage Upstreams, Routes and protos. The screenshots below were taken as nora.becker.

Part one: JSON in, gRPC out

The proto

Open Services › Protos in the Environment’s navigation and select New proto. The page says what the object is for: Routes that transcode JSON to gRPC read their services and messages from here. Name and Description are yours; Content takes the protobuf source text, or a compiled descriptor set as base64. The note under it is the one to read before pasting a file with imports: the content is compiled alone when you save, an import is refused, even of a well-known type, while a descriptor set that carries its imported files is accepted. A file that declares a service needs a package line, or the engine fails the save.

The article pastes the hello service, twelve lines: package hello, HelloRequest with a greeting, HelloResponse with a reply, and HelloService with SayHello.

The New proto page: Name hello-service, Description, the Content field holding the proto3 source of the hello service, and the Changes panel on the right showing the object to be created The proto as an object. The Changes panel shows what reaches the engine when you select Create proto.

Select Create proto. The proto’s page shows the content with its id, which the Plugin below picks from a list.

The Upstream

Under Upstreams, create grpcbin-backend with one node, grpcbin on port 9000, and Scheme grpc. The scheme is how the engine speaks to the nodes, and its help lists the choices: plain or TLS HTTP, gRPC with or without TLS, WebSocket, a raw stream, or Kafka. Without grpc here, the engine would send the transcoded call as HTTP/1.1 to a port that only speaks HTTP/2.

The grpcbin-backend Upstream page: one node grpcbin on port 9000, Balancing type roundrobin, Scheme grpc, and Used by naming the Route hello-grpc The Scheme is the whole difference between an HTTP Upstream and a gRPC one.

The Route and grpc-transcode

Create a Route, hello-grpc, with the path /grpc/hello, the methods GET and POST, and grpcbin-backend as its Upstream. Then Plugins, Add plugin, grpc-transcode:

  • Proto ID: the proto, picked from the Environment’s list, with an Open proto link beside it. The More note is worth reading: an id the gateway does not hold is saved without complaint, and every request then reaches the upstream untranscoded, which a gRPC upstream answers with 502.
  • Service: hello.HelloService, the full name with the package. A name the proto does not define is also saved without complaint, and every request is answered 503.
  • Method: SayHello.
  • Under Behavior, Protobuf options decide how enums, 64-bit integers and absent fields are written into the JSON, at most one word per family; Deadline is sent to the upstream with the call; Show status in body writes the decoded status details of a failed call into the response body as well.

The grpc-transcode form on hello-grpc: Proto ID hello-service · 00000000000000010278 with the Open proto link, Service hello.HelloService, Method SayHello, and the Protobuf options checkboxes under Behavior Three fields name the call. Everything else is how the JSON is written.

Select Add plugin. Two calls with nothing but curl:

### POST /grpc/hello with a JSON body
HTTP/1.1 200 OK
Content-Type: application/json
Trailer: grpc-status
{"reply":"hello g8"}
grpc-status: 0
### GET /grpc/hello?greeting=... the same call as a query
HTTP/1.1 200 OK
Content-Type: application/json
{"reply":"hello from a query"}

The gateway read the body or the query as a HelloRequest, called the service over HTTP/2 and wrote the HelloResponse as JSON; the response also carries the gRPC status as a trailer, grpc-status: 0. The same port asked directly with HTTP/1.1 closed the connection without an answer; curl printed no status at all.

What the transcoder does not do is validate. A body with an unknown field, or a body that is not JSON at all, was turned into an empty HelloRequest and answered {"reply":"hello noname"}, the service’s reply to an empty greeting, with 200. A contract for the JSON side goes on the same Route with request-validation, from Request validation: JSON Schema and OpenAPI at the gateway, and runs before the transcoder.

Traffic Explorer filtered to the path /grpc/hello: four POST records with status 200, 17 to 33 ms, the Route hello-grpc and the Upstream node 172.23.0.11:9000, one of them with the query string greeting=from a query Each call a record, with the gRPC node that answered. The query call is recorded as the POST the gateway sent, the query string kept in the path.

Part two: a Route that upgrades to WebSocket

Create a Route, ws-echo, with the path /ws/echo and httpbin-backend as its Upstream, with proxy-rewrite sending /ws/echo to the backend’s /websocket/echo. Before anything else is set, a WebSocket handshake through the gateway:

### WebSocket handshake through the gateway, Websocket off on the Route
HTTP/1.1 400 Bad Request
{"status_code": 400, "error": "Bad Request", "detail": "missing required `Upgrade: websocket` header"}

The backend answered, and it did not see the upgrade: the gateway forwarded the request as plain HTTP and left the Upgrade header behind. Open the Route, Edit route, and under Target, beside the Upstream, switch Websocket on; its help is one line, it lets a matching request upgrade the connection to WebSocket.

The Target section of the ws-echo Route form: Upstream selected with httpbin-backend · 00000000000000010210 and the Open upstream link, and the Websocket switch on with the help line "Lets a matching request upgrade the connection to WebSocket." One switch on the Route. The Upstream stays an HTTP one; the connection is upgraded on the way through.

Select Save route. The same handshake, and then a client:

### the same handshake, Websocket on
HTTP/1.1 101 Switching Protocols
Connection: upgrade
Upgrade: websocket
### from a browser page: new WebSocket("ws://localhost:9281/ws/echo"), three messages
open after 34 ms
echo: hello from the browser
echo: second message
echo: {"order":"ord-10421","status":"open"}
closed 1000

The client script is the four lines any browser runs: open the socket at the gateway’s address, send on open, read on message, close. Three messages went to the backend and came back through the gateway. For a message size limit, the Plugin websocket-proxy sets a client and an upstream limit on the Route; it was not needed here.

Traffic Explorer filtered to the path /ws/echo: a 101 record of 27 ms and a 101 record of 5.01 s, both GET with the Route ws-echo and the backend's address, and the earlier 400 record of 19 ms A WebSocket connection is one record with status 101, written when it closes; its latency is how long the connection stayed open, 5.01 s for the one curl held.

What the audit log keeps

Creating the proto is a proto.created record with the whole content as the engine received it; the Upstream is an upstream.created record with the node and the scheme; the Routes are route.created and, for the switch, a route.updated with enable_websocket as the difference. None of these objects holds a secret.

The Change section of the audit record of the proto creation: the After side with id, desc, name and the proto3 content of the hello service on one line The proto as the engine holds it. A later edit of the service is a second record with the old and the new content.

What to do next

Written by
Akif F. Güven
Engineer, G8
Was this helpful?
Next step

See G8 in a demo

We’ll show you the console with APIs and AI traffic like yours, and answer the questions these articles leave open.

  1. 01Demo
  2. 02Proof of conceptOn Docker for a quick start, or on the infrastructure you plan to run G8 on.
  3. 03LicenseWe prepare a quote for your edition and nodes, and you receive a signed license file.