Skip to main content

Messaging

Copy-Paste Example

Message handler file:
Microservice boot file:
Caller:

More Examples

Typed fixed-path operations

Use defineMicroContract() and createMicroClient() from @hile/micro-contract to share stable JSON schemas and typed remote calls. Implement each operation in its matching message file with defineMicroMessage(operation, handler), then call loadMicroContract(app, contract, messagesDirectory) from @hile/micro to obtain { local, activate, close }. The binding validates filesystem paths and complete implementation coverage before activation. Explicit local calls are same-service unary calls through the provider executor, not a namespace-based optimization; normal calls still use RPC. Typed remote calls default to zero retries. Native dynamic and streaming APIs remain supported. See Typed Micro contracts for the full example, schema constraints, and shutdown ordering.

Generic loader protocol registration

File loading preserves definition metadata automatically. Raw registration must pass the metadata explicitly. Ordinary definitions/dispatch omit protocol on both sides. Protocol tags are nonempty printable ASCII strings, at most 128 characters; registration snapshots the value. Neither payload fields nor Context/extras can select the dispatch protocol. After one route match, a different protocol throws MessageProtocolMismatchError (HILE_MESSAGE_PROTOCOL_MISMATCH) before the handler runs, with no fallback to another matching route. A tag prevents accidental protocol misdelivery, not unauthorized invocation by a caller able to set the tag.

Native streaming and custom transports

Streaming handler:
Streaming caller:
Streaming request body with a normal response:
If no structured metadata is needed, pass an AsyncIterable, Uint8Array, or ArrayBuffer directly as data; Hile sends it as the request input stream and the handler receives data === undefined. app.stream() accepts the same request input forms when both request and response need to stream. Custom WebSocket modem:
Notice that request() returns a Promise<T>. Await it directly.

Complete Example

Provider handler:
Untyped dynamic handlers may use a required catch-all when the operation is intentionally not a typed Micro Contract:
Typed Micro Contract operation.path values stay fixed and cannot use [id] or [...paths]. Provider boot:
Consumer:

Anti-Patterns

  • Appending a secondary response getter to client.request('/x', data)
  • Returning a plain object from a handler called through stream().
  • Retrying a consumed request stream or hiding it inside a replay-unsafe factory.
  • Branching business code on whether a Micro target namespace is local or remote.
  • Using pub/sub as a durable queue.
  • Forgetting to register shutdown(await app.listen(...)).