Kobolds

Architecture#

Kobolds is organized as a monorepo containing the broker, client libraries, libkobolds, and command-line tooling.

The broker is the central runtime component. It accepts connections from clients, authenticates and authorizes operations, maintains broker state, and routes messages between connected peers.

A Kobolds deployment can run in several forms:

  • A broker and its clients on the same machine, such as an embedded, edge, or local-development deployment.
  • A standalone broker on a separate machine serving multiple client processes.
  • A multi-broker deployment when clustering is configured and supported.

This page describes the high-level relationship between clients, broker connections, workers, and the router. For the bytes exchanged over a connection, see the protocol reference.

System overview#

The following example contains one Kobolds broker and three connected clients:

  • Client A is an IoT device that publishes telemetry and replies to commands.
  • Client B is another IoT device with the same role.
  • Client C is a controller that subscribes to telemetry and sends commands.
  • The broker owns connections for each client and assigns those connections to workers.
  • Workers communicate with the central router, which coordinates stateful broker operations and message routing.
flowchart LR subgraph Broker["Kobolds Broker"] direction TB subgraph RouterThread["Router Thread"] Router["Router"] end subgraph WorkerAThread["Worker A Thread"] direction TB WorkerA["Worker A"] ConnA["Connection A"] end subgraph WorkerBThread["Worker B Thread"] direction TB WorkerB["Worker B"] ConnB["Connection B"] ConnC["Connection C"] end subgraph ListenersThread["Listeners Thread"] TCPListener["TCP Listener"] end ConnA <--> WorkerA ConnB <--> WorkerB ConnC <--> WorkerB WorkerA <--> Router WorkerB <--> Router TCPListener <--> Router end subgraph ClientA["Client A: IoT Device"] direction TB PublisherA["Publisher
/device/a/telemetry"] ReplierA["Replier
/device/a/command"] end subgraph ClientB["Client B: IoT Device"] direction TB PublisherB["Publisher
/device/b/telemetry"] ReplierB["Replier
/device/b/command"] end subgraph ClientC["Client C: Controller"] direction TB SubscriberA["Telemetry subscriber
/device/*/telemetry"] RequestorA["Requestor
/device/a/command"] RequestorB["Requestor
/device/b/command"] end ClientA <--> ConnA ClientB <--> ConnB ClientC <--> ConnC

Example message flow#

The diagram shows two common messaging patterns: publish/subscribe and request/reply.

Telemetry publish and subscribe#

Client A publishes telemetry to:

/device/a/telemetry

Client B publishes telemetry to:

/device/b/telemetry

Client C subscribes using the pattern:

/device/*/telemetry

The * segment matches exactly one path segment. The controller subscription therefore matches both device telemetry topics:

/device/a/telemetry
/device/b/telemetry

When either IoT client publishes a matching telemetry message, the broker routes a delivery to Client C.

Client A or Client B
        │
        │ publish telemetry
        ▼
Kobolds broker
        │
        │ topic matches /device/*/telemetry
        ▼
Client C telemetry subscriber

Device command request and reply#

Each IoT client registers a replier for its command service:

/device/a/command
/device/b/command

The controller creates requestors for those services. It can send a request to a device-specific service and receive the corresponding reply.

Controller
    │
    │ request /device/a/command
    ▼
Kobolds broker
    │
    │ route to matching replier
    ▼
IoT Device A
    │
    │ reply
    ▼
Kobolds broker
    │
    │ route reply to originating requestor
    ▼
Controller

The broker uses request and delivery metadata to associate a reply with the requestor that initiated the request.

Broker components#

A broker process starts a router thread, one or more worker threads, and supporting runtime components such as tracing or observability infrastructure.

Broker process
├─ Router thread
├─ Worker threads
├─ Connection state
├─ Session and entity state
└─ Optional tracing and observability components

The exact number of workers and enabled runtime components depends on broker configuration and host resources.

Router#

The router is the central coordinator for stateful broker operations. It owns or coordinates operations that need a consistent view of broker state.

Typical router responsibilities include:

  • Authenticating new connections
  • Authorizing client actions
  • Creating, reading, and deleting broker-managed entities
  • Managing session-owned state
  • Resolving publishers, subscribers, requestors, and repliers
  • Matching topics and services against patterns
  • Routing messages and responses
  • Producing protocol-level errors

The router is designed to keep coordination work centralized. Operations that perform transport I/O, serialization, deserialization, or other connection work are handled by workers.

Workers#

Workers manage active client connections. Each worker runs independently from the router and can own multiple connections.

Typical worker responsibilities include:

  • Accepting or registering connections assigned to the worker
  • Reading bytes from active transports
  • Parsing frames and decoding messages
  • Serializing messages and frames for outbound delivery
  • Buffering and batching network I/O
  • Detecting connection closure and transport errors
  • Passing inbound messages to the router
  • Sending router-directed messages to the appropriate connection

Workers and the router exchange messages through non-blocking queues. This allows the router to coordinate broker state without directly performing transport I/O for every client connection.

Connections#

A connection is the active communication path between a client and the broker. A worker owns the connection’s transport-level work.

A connection is responsible for the mechanics of data transfer:

  • Receiving bytes from a client
  • Parsing incoming frames
  • Queuing outbound frames
  • Writing bytes to the transport
  • Reporting connection lifecycle events to the broker

A connection should not be confused with a session:

  • A connection is the active transport path.
  • A session is the logical broker-side state associated with an authenticated client or peer.

Sessions and entities#

The broker tracks a session for each authenticated client or peer. The session records the state and resources associated with that peer.

Entities are broker-managed resources owned by, or associated with, a session. Examples include publishers, subscribers, requestors, repliers, topics, and services.

This relationship lets the broker:

  • Authorize actions against session-owned resources
  • Route messages to the appropriate entities
  • Clean up resources when a client disconnects
  • Associate requests, deliveries, and replies with their originating client

For the entity model and protocol operations, see the protocol reference.

Client architecture#

A Kobolds client is the application-side library or runtime component that communicates with a broker.

Application
    │
    ▼
Kobolds client library
    │
    ▼
Transport connection
    │
    ▼
Kobolds broker

A client typically provides application-facing APIs for:

  • Opening and closing broker connections
  • Completing authentication
  • Creating entities
  • Publishing messages
  • Receiving subscribed messages
  • Sending requests
  • Receiving replies
  • Handling broker errors and connection events

The client translates application operations into protocol messages, serializes them into frames, and sends the resulting bytes through the active transport. For inbound data, it performs the reverse operation:

Inbound bytes
    -> frames
    -> messages
    -> client events or callbacks
    -> application code

Client libraries may use language-appropriate concurrency and I/O models. For example, a client may integrate with an event loop, asynchronous runtime, or native non-blocking I/O implementation while preserving the same Kobolds protocol behavior.

Deployment considerations#

Running the broker and clients on the same machine can reduce network hops and simplify local deployments. Running the broker independently can centralize routing and allow multiple machines or processes to communicate through one shared broker.

Choose a deployment model based on:

  • Latency requirements
  • Failure-isolation requirements
  • Network topology
  • Operational ownership
  • Resource limits on client hosts
  • Whether clients must communicate across machines or networks

The protocol and routing model remain the same regardless of whether a client connects locally or across the network.