Skip to main content

Call a gRPC Service from MQTT

A gRPC connector opens a channel from the broker to an external gRPC service and maps MQTT topics onto that service’s calls. Publish bytes on a source topic and the connector sends them; replies are published back onto a destination topic.
Like a phone line to another building. The connector keeps one line open to the service (the channel). Each conversation on that line is a call: MQTT bytes go in, and the reply bytes come back on another topic.

When to use it

  • A service you already run speaks gRPC, and MQTT messages should invoke it without a separate gateway.
  • Each device needs its own long-lived stream. With the default session scope, one IMEI (or any other id in the source topic) gets its own bidirectional call.
  • The exchange is one request and one reply, a server stream, a client stream, or a bidirectional stream.
Use a REST API connector for HTTP. Use the Kafka connector for an event log. Encode and decode protobuf with LoT models — this connector forwards raw bytes and does not read a .proto file.
Define the channel once, then map the MQTT topics that feed it.

Quick Start

One MQTT message opens one call and publishes the single reply:
A payload on grpc/echo/in is the request. The response bytes are published to grpc/echo/out.
USE_TLS "false" is for a service that speaks plaintext HTTP/2. The default is TLS on port 443. Use 127.0.0.1 only when that service listens on the broker host. Set ADDRESS with GET ENV and credentials with GET SECRET.

How a Call Is Chosen

One connector is one channel to the service. HTTP/2 carries many calls on that channel. An event can set its own SERVICE, METHOD, RPC_TYPE, DEADLINE, and METADATA, so two methods can share the channel. Add another connector when the host is different. There is no session-key setting. Put application ids inside the payload. The connector correlates calls with the MQTT topic match. ADD CONNECTOR_CONFIG still controls wait-or-drop when this connector falls behind incoming MQTT (ADD ROUTE_CONFIG remains a supported alias). See Store and Forward Configuration. Its HOT_RELOAD setting is false by default on gRPC, so a changed GET ENV or GET SECRET value outside the token settings does not cut an open stream. It applies when the connector next starts. See Changed values in running connectors.

GRPC_CONFIG

The parser also accepts SERVERSTREAM, CLIENTSTREAM, BIDIRECTIONAL, and DUPLEX for RPC_TYPE, and SOURCE, SOURCE_TOPIC, SHARED, and ROUTE for SESSION_SCOPE. Use the spellings in the table.

Event parameters

Several ADD EVENT blocks share the same channel. Each event keeps its own calls. ADDRESS, PORT, USE_TLS, CLIENT_AUTH, token settings, SESSION_SCOPE, and IDLE_TIMEOUT stay on GRPC_CONFIG. A route whose resolved service or method is empty does not add, and the error names the event. Response publishes use QoS 0 and are not retained. A failed unary call publishes one JSON object on DESTINATION_TOPIC and is still a route error. A server-stream or bidirectional response stream does the same when it ends with a gRPC status other than a normal close, and so does a client-stream call whose response fails. Invalid argument and aborted are that close on a bidirectional stream: they stay a warning and do not publish the JSON. After the unauthenticated retry, any other gRPC status is published the same way.
statusCode is the gRPC status name. error is the status detail only. A successful response stays the raw bytes. Two methods can share one channel. Add another connector when the host is different.

Example: Google Bigtable

This example reads and would mutate rows on bigtable.googleapis.com. Put admin calls such as GetTable on bigtableadmin.googleapis.com in a second route. Store the service-account JSON with -setSecret and reference it with GET SECRET. Replace the project, instance, and table placeholders.
Encode the request and read a ReadRows chunk with protobuf models. The broker does not ship Bigtable .proto files.

RPC Types

An empty MQTT payload is a real empty request for UNARY and SERVER_STREAM. For CLIENT_STREAM and BIDI it is the half-close (no more client messages). The connector then waits up to 30 seconds for the remaining server bytes so the last response can still be published. PAYLOAD AS BYTES in an Action does not publish an empty payload. Send the zero-length message from an MQTT client (MQTT Explorer or any MQTT client) on that source topic. Raise DEADLINE above the longest call you expect. The default of 30 seconds ends a bidirectional session that runs longer than that, even when messages are still flowing. Raise IDLE_TIMEOUT when silence between messages is normal, or set it to 0 so only an empty payload half-closes the call.

Client stream

Publish request bytes to grpc/upload/in. Publish an empty payload, or wait for IDLE_TIMEOUT, to half-close. The single response is published to grpc/upload/out.

One shared call (SESSION_SCOPE EVENT)

SESSION_SCOPE "EVENT" keeps a single bidirectional call for every topic that matches the event. A new IMEI does not open its own call. Replies are published with {topic.N} taken from the latest source topic, not from the topic that opened the call.
Leave the default TOPIC when each device must stay isolated. Use EVENT only when the service expects one shared stream.
After a bidirectional call has already ended, further payloads on that session are dropped until you publish an empty payload. The next non-empty payload then opens a fresh call. A service that ends the turn inside the protobuf, without an empty MQTT message, leaves that topic dropped until the empty payload arrives. The connector logs a warning with the topic and the number of bytes it dropped. That end-of-stream condition (InvalidArgument or Aborted) does not by itself mark the connector unhealthy.

Authentication

CLIENT_AUTH attaches credentials as initial metadata on each call. Access tokens are not published on MQTT topics. If the service returns unauthenticated, the connector drops the token that call sent, drops the streaming call, and retries that payload once so a new token is on a new call. If a CLIENT_STREAM or BIDI call that sent a fetched token (the OAUTH2_* and GOOGLE_METADATA modes) is refused as unauthenticated before the service answers, the connector opens it again once with a new token and sends the messages it had already written, up to 64. A refusal after the service answered, past 64 messages, or again on the reopened call is published as a gRPC error, the token is dropped, and the next message opens a new call with a new token. A refused server-stream also drops the token, so the next message opens a new call. When several calls are refused at once, the connector fetches one new token for all of them. TOKEN_AUTH chooses how the client authenticates to the token endpoint: CLIENT_SECRET_BASIC (default), CLIENT_SECRET_POST, or PRIVATE_KEY_JWT (CLIENT_ID plus CLIENT_ASSERTION_KEY as PEM or JWK). ISSUER is the OIDC issuer; the connector discovers the token endpoint when TOKEN_URL is omitted. BEARER_TOKEN and API_KEY also accept the setting name AUTH_SECRET. An API_KEY, or AUTH_SECRET when that supplies the API key, written as GET ENV, GET SECRET, ENV:, or SECRET: is resolved on each call. BEARER_TOKEN, SERVICE_ACCOUNT, CLIENT_SECRET, REFRESH_TOKEN, and CLIENT_ASSERTION_KEY keep the same kind of reference and are read when a token is acquired. A later -setSecret, -removeSecret, -setEnv, KEEP ENV, or KEEP SECRET on one of these drops the cached token, and the next call uses the new value. The channel stays up, so you do not need to re-add the route. A change to the address or any other setting applies when the connector next starts, unless you set WITH HOT_RELOAD true in ADD CONNECTOR_CONFIG. See Changed values in running connectors. If the reference is empty, the call goes out without the key and the broker logs one warning that names the reference, not the value. For OAUTH2_JWT_BEARER, set TOKEN_URL when the service-account JSON has no token_uri. For GOOGLE_METADATA, the default metadata address is http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token. Set METADATA_URL to point elsewhere. When SCOPE is set and that URL has no query string, the connector adds the scope as a scopes query parameter.
Send an API key in initial metadata. GET SECRET and GET ENV are read on each call:
Extra metadata is merged with the auth entry. Prefer a JSON object when a value contains a comma:
The connector fetches the access token on the first call that needs it, not when it starts, so a route starts even when its token endpoint is down. A channel failure leaves the connector disconnected. Startup waits up to 10 seconds for the channel. If the route has not started, the broker retries in the background, from 2 seconds up to 60 seconds, until the route starts or you remove it. Removing the connector drops the cached token. There is no setting for a client certificate or a private CA file. TLS uses the trust store on the broker host.

Token fetch failures

When a call needs a new token and the token endpoint cannot be reached, does not answer in time, or answers 408, 429, or a 5xx status, the connector tries up to 3 times in total within the call’s deadline. If every attempt fails, the message fails as UNAVAILABLE. Any other 4xx answer, such as a rejected client secret, fails the message as UNAUTHENTICATED at once, without a retry. Either failure is published on the event’s destination topic like any other gRPC error, and the next message tries again:
Configured secrets and tokens are masked in the published error and in the broker log, including where a token endpoint echoes them back.

Payloads

The connector sends and publishes the MQTT payload as raw bytes. It does not encode JSON and it does not compile protobuf. A binary publish stays binary. If the topic cache has no raw bytes, the connector sends the payload text as UTF-8, which corrupts protobuf and audio. Publish protobuf with PUBLISH MODEL, or forward an existing blob with PAYLOAD AS BYTES. Decode replies with GET PROTO on the destination topic. The model describes the request. The Action writes binary protobuf to the topic the connector is listening on:
EchoGrpc from the Quick Start then forwards those bytes and publishes the service’s reply on grpc/echo/out. Read fields from that reply with GET PROTO and a response model. See Protobuf models.

Check the Round Trip

1

Deploy the connector

Deploy the LoT, then confirm the connector reaches the service. Health is retained on $SYS/Coreflux/Routes/EchoGrpc/status. See Connector status.
2

Subscribe to the reply

In MQTT Explorer, subscribe to the destination topic (grpc/echo/out, or devices/+/downlink for the per-device example).
3

Publish a request

Publish a payload to the source topic. For a unary echo, the reply is one message. For a bidirectional device topic, further publishes on that same topic reuse the call until an empty payload or the idle timeout.
4

Half-close a stream

For CLIENT_STREAM or BIDI, publish a zero-length payload on that source topic. The last server bytes are published, and the next non-empty payload opens a new call.

Best Practices

  • Keep SESSION_SCOPE "TOPIC" (the default) so each device topic, such as one IMEI, has its own call.
  • Set DEADLINE to the full length of a streaming call, not the gap between messages.
  • Store keys with GET SECRET. Do not publish tokens on MQTT topics.
  • Put correlation ids in the payload. The connector will not add a session id for you.
  • Tell operators that an empty payload half-closes CLIENT_STREAM and BIDI.
  • Do not send metadata keys that start with grpc-.

Limitations

  • Raw bytes only. The connector does not load a .proto or a protobuf codec. Encode before publish and decode after the reply.
  • Deadline is fixed when the call opens. A 30-second deadline stops a longer bidirectional call.
  • Finished bidirectional calls drop new bytes until an empty payload half-closes that session.
  • SESSION_SCOPE "EVENT" shares one call. Replies follow the latest source topic.
  • No client certificate or private CA field. TLS uses the broker host trust store.
  • Replies are QoS 0 and not retained.
  • Empty binary publishes from LoT do not half-close. PAYLOAD AS BYTES does not publish an empty payload. Publish the zero-length message from an MQTT client.

Troubleshooting

  • GRPC_CONFIG needs ADDRESS. Each event needs SOURCE_TOPIC, DESTINATION_TOPIC, and a resolved SERVICE and METHOD (on the event or on GRPC_CONFIG).
  • A DEADLINE that is not a positive whole number, an unknown RPC_TYPE, or missing JWT material for OAUTH2_JWT_BEARER fails the add. A failed add does not log that the route initialized.
  • The channel must connect within 10 seconds. Use a host and port the broker host can reach, and match USE_TLS to the service (plaintext often uses port 50051; TLS often uses 443).
  • If the route fails to start, the broker retries in the background, from 2 seconds up to 60 seconds, until the route starts or you remove it.
  • A token endpoint that is down does not stop the route starting. OAuth modes fetch a token on the first call. See Token fetch failures.
The connector retries once with a new token on a new streaming call, and a refused client-stream or bidirectional call sends its messages again. See Authentication. If that retry fails, the destination topic receives one JSON error with success, statusCode, and error. UNAUTHENTICATED can also mean the token endpoint rejected the credentials, and UNAVAILABLE that it could not be reached. See Token fetch failures. Check CLIENT_AUTH, the token endpoint, and that the secret resolves. Tokens are not written to MQTT topics.
After the unauthenticated retry, any other gRPC status, including on a server stream, publishes one JSON object on the event’s destination topic: success, statusCode (the status name), and error (the status detail, with secrets and tokens masked). A retry that fails without a gRPC status publishes UNKNOWN. A successful response stays the raw bytes.
Publish raw bytes (PUBLISH MODEL or PAYLOAD AS BYTES). A text payload is sent as UTF-8.
Remove any key that starts with grpc-. Use a JSON object for METADATA when a value contains a comma.
Nothing published an empty payload, and IDLE_TIMEOUT is high or 0. Publish a zero-length payload on the source topic, or lower IDLE_TIMEOUT.
The previous bidirectional call already ended. Payloads on that topic are dropped until an empty payload. Publish the empty payload, then send the next request. Check connector logs for the dropped-byte warning.
SESSION_SCOPE "EVENT" expands {topic.N} from the latest source topic. Use TOPIC when each IMEI needs its own call and its own reply topic.

Next Steps

Protobuf Models

Encode request bytes with PUBLISH MODEL and read replies with GET PROTO.

Kafka Connector

Stream MQTT to an event log when the other side is Kafka, not a gRPC method.
Last modified on December 4, 2026