Your first federate in Python

This tutorial takes you from an empty directory to two Python programs that talk to each other through an HLA 4 RTI (IEEE 1516-2025). You install the microrti package, run two federates in one script on an RTI inside the process, then run them as two processes connected to a microrti server over the Federate Protocol. You do not need to know HLA: each idea is explained where it first appears, and The concepts of HLA, one by one has the longer version.

What you need

  • Python 3.11 or later. Check with python3 --version: some systems still ship an older python3.
  • On Linux, x86-64 or ARM64, nothing else: you install a prebuilt wheel. On macOS and other systems, a Rust toolchain, version 1.88 or later (rustc --version), installed from rustup.rs: part of the package is written in Rust, and there pip compiles it.
  • For the second half, the microrti server. Install and run the RTI explains the options: a static binary from the MicroRTI release on Linux, cargo install microrti elsewhere.

Install the package

The package is not on PyPI. You install version 0.3.0 from its release on GitHub, into a virtual environment in a new directory:

mkdir greetings
cd greetings
python3.11 -m venv .venv
. .venv/bin/activate

Use any interpreter of version 3.11 or later in the third line (python3.12, python3.13, or plain python3 if it is recent enough).

On Linux with glibc 2.28 or later (Debian 10, Ubuntu 20.04, RHEL 8 and later), install the wheel for your processor, which uname -m names (x86_64 or aarch64):

pip install "https://github.com/microrti/microrti-python/releases/download/v0.3.0/microrti-0.3.0-cp311-abi3-manylinux_2_28_$(uname -m).whl"

On Alpine and other musl-based systems, write musllinux_1_2 in place of manylinux_2_28. One wheel serves every Python version from 3.11 on.

On macOS and elsewhere, pip builds the package from the source archive of the release:

pip install https://github.com/microrti/microrti-python/releases/download/v0.3.0/microrti-0.3.0.tar.gz

It fetches the build tool, maturin, and the MicroRTI crates from crates.io, then compiles them. On the ARM Mac this page was tested on, that took about a minute (five minutes of processor time spread over the cores). The output ends with:

Successfully built microrti
Installing collected packages: microrti
Successfully installed microrti-0.3.0

Check that Python finds it:

python -c 'import microrti; print(microrti.__version__, microrti.HLA_VERSION)'
0.3.0 IEEE 1516-2025

Keep the virtual environment active for the rest of the tutorial: in every new terminal, run . .venv/bin/activate from the greetings directory first. Put the files below in that directory too.

Describe what the federates exchange

Programs that take part in an HLA simulation are called federates. They agree beforehand on the kinds of data they exchange, and that agreement is the Federation Object Model (FOM): an XML file that the RTI reads when a run of the simulation is set up. The FOM below declares a single interaction class. An interaction is a message sent once, like an event. The other kind of shared data in HLA, object instances whose attributes are updated over time, is not needed here.

Save this as greetings.xml:

<?xml version="1.0" encoding="UTF-8"?>
<objectModel xmlns="http://standards.ieee.org/IEEE1516-2025">
  <modelIdentification>
    <name>Greetings</name>
    <type>FOM</type>
    <version>1.0</version>
    <description>One interaction class: a line of text sent by a federate</description>
  </modelIdentification>
  <interactions>
    <interactionClass>
      <name>HLAinteractionRoot</name>
      <interactionClass>
        <name>Greeting</name>
        <sharing>PublishSubscribe</sharing>
        <transportation>HLAreliable</transportation>
        <order>Receive</order>
        <parameter>
          <name>Sender</name>
          <dataType>HLAunicodeString</dataType>
        </parameter>
        <parameter>
          <name>Text</name>
          <dataType>HLAunicodeString</dataType>
        </parameter>
      </interactionClass>
    </interactionClass>
  </interactions>
</objectModel>

Greeting has two parameters, Sender and Text, both of the standard data type HLAunicodeString. PublishSubscribe lets a federate both send and receive the class, HLAreliable asks for a transport that does not drop messages, and Receive order delivers interactions in the order they arrive, with no logical time involved. The other order, by timestamp, is the subject of Logical time, explained.

Two federates in one script

The package contains an RTI that runs inside your Python process, EmbeddedRti. Federates that use it reach it by function calls: there is no server to start and no network. It is the quickest way to try an idea, and a good way to test a federate.

Save this as hello.py, next to greetings.xml:

"""Two federates, alice and bob, in one script, on an RTI inside the process."""

from pathlib import Path

from microrti import CallbackModel, EmbeddedRti, FederateAmbassador, ResignAction
from microrti.encoding import decode, encode

FOM = Path(__file__).parent / "greetings.xml"


class Inbox(FederateAmbassador):
    """Bob's federate ambassador: the RTI calls its methods to tell bob what happened."""

    def __init__(self):
        self.messages = []

    def receive_interaction(
        self,
        interaction_class,
        parameter_values,
        user_supplied_tag,
        transportation_type,
        producing_federate,
        sent_regions,
    ):
        self.messages.append(parameter_values)


embedded = EmbeddedRti()

# 1. Connect to the RTI. Alice receives no callbacks in this script, hence None.
alice = embedded.ambassador()
alice.connect(None, CallbackModel.EVOKED)
inbox = Inbox()
bob = embedded.ambassador()
bob.connect(inbox, CallbackModel.EVOKED)

# 2. Create the federation execution from the FOM, then join it.
alice.create_federation_execution("Greetings", FOM)
alice.join_federation_execution_with_name("alice", "greeter", "Greetings")
bob.join_federation_execution_with_name("bob", "greeter", "Greetings")
print("alice and bob joined Greetings")

# 3. Look up the handles of the class and its parameters, by their names in the FOM.
greeting = alice.get_interaction_class_handle("Greeting")
sender = alice.get_parameter_handle(greeting, "Sender")
text = alice.get_parameter_handle(greeting, "Text")

# 4. Declare what each federate sends and wants to receive.
alice.publish_interaction_class(greeting)
bob.subscribe_interaction_class(greeting)

# 5. Alice sends one interaction; the values are encoded as the FOM says.
alice.send_interaction(
    greeting,
    {sender: encode("HLAunicodeString", "alice"), text: encode("HLAunicodeString", "Hello, bob")},
)

# 6. Bob asks the RTI for his callbacks until the greeting has arrived.
while not inbox.messages:
    bob.evoke_multiple_callbacks(0.01, 0.1)
for values in inbox.messages:
    who = decode("HLAunicodeString", values[sender])
    what = decode("HLAunicodeString", values[text])
    print(f"bob received from {who}: {what}")

# 7. Leave: resign, destroy the federation execution, disconnect.
alice.resign_federation_execution(ResignAction.NO_ACTION)
bob.resign_federation_execution(ResignAction.NO_ACTION)
alice.destroy_federation_execution("Greetings")
alice.disconnect()
bob.disconnect()
print("done")

Run it:

python hello.py
alice and bob joined Greetings
bob received from alice: Hello, bob
done

What the script does

The numbers match the comments in the script. Clause numbers refer to IEEE 1516.1-2025, the federate interface; every method’s docstring names its service and clause too (help(bob.send_interaction)).

  1. Each federate gets its own RTI ambassador from embedded.ambassador(): the object you call when you want something from the RTI. Connect (4.2) also takes the federate ambassador, the object the RTI calls back to tell the federate what happened. Bob’s is Inbox, a subclass of FederateAmbassador that overrides receive_interaction; the 61 other callback methods do nothing unless you override them. Alice passes None because she expects nothing. With CallbackModel.EVOKED, callbacks wait until the federate asks for them, in step 6.
  2. A federation execution is one named run of a simulation, which federates join and leave. Create Federation Execution (4.5) creates Greetings from the FOM; creating it a second time raises FederationExecutionAlreadyExists. Join Federation Execution (4.11) takes the federate name, which must be unique in the federation execution, the federate type, free text that says what kind of program this is, and the name of the federation execution.
  3. The RTI refers to classes and parameters by handles. You look each one up once, by its name in the FOM (10.13, 10.15), and use it from then on. Handles hold for the whole federation execution, which is why the script lets bob use the handles alice looked up. A misspelt name raises NameNotFound.
  4. Publishing a class (5.4) declares that the federate sends it; subscribing (5.10) declares that it wants to receive it. The RTI delivers an interaction to the federates subscribed to its class, and refuses to send a class the federate has not published (InteractionClassNotPublished).
  5. Send Interaction (6.12) takes a dict from parameter handle to bytes. The RTI never looks inside the bytes: federates agree on their layout through the data types of the FOM, and microrti.encoding converts Python values to and from them.
  6. evoke_multiple_callbacks(0.01, 0.1) (10.58) runs pending callbacks on the calling thread, for at least 0.01 and at most 0.1 seconds. Bob’s receive_interaction, the Receive Interaction callback (6.13), runs inside that call. The loop repeats until the greeting is there.
  7. Each federate resigns (4.12). ResignAction.NO_ACTION is enough, since neither owns any object instance. Destroy Federation Execution (4.6) works once nobody is joined, and raises FederatesCurrentlyJoined before that. Disconnect (4.3) ends the connection.

Each exception of the standard is a Python class, all of them subclasses of microrti.RtiError, so except microrti.FederationExecutionAlreadyExists: catches exactly that case.

The same federates through a server

Now the RTI runs as a program of its own, and alice and bob are two processes. They could be on different machines; here everything runs on one.

Start the RTI

In a first terminal:

microrti --listen tcp://127.0.0.1:0

Port 0 lets the system pick a free port. The server prints the address it got on its standard output, in the listening on line, and its log on the standard error:

2026-12-02T21:05:32.215971Z  INFO microrti: microrti starting version="0.5.0"
2026-12-02T21:05:32.218977Z  INFO microrti::server: FOM modules and MIMs named by file:, http: or https: URL are read wherever they are: set fom_urls = "none" or fom_url_directory to restrict this when federates outside a trusted network can reach the RTI timeout=30s
2026-12-02T21:05:32.219182Z  INFO microrti::server: federation saves are kept in memory and lost when the RTI stops: set save_directory to keep them in files
2026-12-02T21:05:32.221680Z  INFO microrti_fedpro::server: Federate Protocol listener ready address=127.0.0.1:59144 transport=tcp
2026-12-02T21:05:32.221602Z  INFO microrti::server: RTI listening endpoint=tcp://127.0.0.1:59144
listening on tcp://127.0.0.1:59144

Your port number will be different; use yours below. Without --listen, the server listens on port 15164 of every network interface, and tcp://localhost:15164 is the address the two scripts use when you give them none. Ctrl-C stops the server.

Write bob and alice

Bob listens. Save this as listener.py:

"""bob: joins the federation execution Greetings on an RTI server and prints what he receives."""

import contextlib
import sys
from pathlib import Path

import microrti
from microrti import CallbackModel, FederateAmbassador, ResignAction, RtiAmbassador
from microrti.encoding import decode

FOM = Path(__file__).parent / "greetings.xml"
address = sys.argv[1] if len(sys.argv) > 1 else "tcp://localhost:15164"


class Inbox(FederateAmbassador):
    def __init__(self):
        self.messages = []

    def receive_interaction(
        self,
        interaction_class,
        parameter_values,
        user_supplied_tag,
        transportation_type,
        producing_federate,
        sent_regions,
    ):
        self.messages.append(parameter_values)


inbox = Inbox()
bob = RtiAmbassador()
bob.connect_with_configuration(inbox, CallbackModel.EVOKED, address)
with contextlib.suppress(microrti.FederationExecutionAlreadyExists):
    bob.create_federation_execution("Greetings", FOM)
bob.join_federation_execution_with_name("bob", "greeter", "Greetings")

greeting = bob.get_interaction_class_handle("Greeting")
sender = bob.get_parameter_handle(greeting, "Sender")
text = bob.get_parameter_handle(greeting, "Text")
bob.subscribe_interaction_class(greeting)
print(f"bob: joined Greetings at {address}, waiting", flush=True)

finished = False
try:
    while not finished:
        bob.evoke_callback(1.0)
        while inbox.messages:
            values = inbox.messages.pop(0)
            who = decode("HLAunicodeString", values[sender])
            said = decode("HLAunicodeString", values[text])
            print(f"bob: {who} says {said!r}", flush=True)
            finished = finished or said == "bye"
except KeyboardInterrupt:
    print("bob: interrupted", flush=True)

bob.resign_federation_execution(ResignAction.NO_ACTION)
with contextlib.suppress(microrti.FederatesCurrentlyJoined):
    bob.destroy_federation_execution("Greetings")
bob.disconnect()
print("bob: left", flush=True)

Alice talks. Save this as talker.py:

"""alice: joins the federation execution Greetings on an RTI server and says each argument."""

import contextlib
import sys
from pathlib import Path

import microrti
from microrti import CallbackModel, ResignAction, RtiAmbassador
from microrti.encoding import encode

FOM = Path(__file__).parent / "greetings.xml"
address = sys.argv[1] if len(sys.argv) > 1 else "tcp://localhost:15164"
lines = sys.argv[2:] or ["Hello, bob", "bye"]

alice = RtiAmbassador()
alice.connect_with_configuration(None, CallbackModel.EVOKED, address)
with contextlib.suppress(microrti.FederationExecutionAlreadyExists):
    alice.create_federation_execution("Greetings", FOM)
alice.join_federation_execution_with_name("alice", "greeter", "Greetings")

greeting = alice.get_interaction_class_handle("Greeting")
sender = alice.get_parameter_handle(greeting, "Sender")
text = alice.get_parameter_handle(greeting, "Text")
alice.publish_interaction_class(greeting)

for line in lines:
    alice.send_interaction(
        greeting,
        {sender: encode("HLAunicodeString", "alice"), text: encode("HLAunicodeString", line)},
    )
    print(f"alice: said {line!r}", flush=True)

alice.resign_federation_execution(ResignAction.NO_ACTION)
with contextlib.suppress(microrti.FederatesCurrentlyJoined):
    alice.destroy_federation_execution("Greetings")
alice.disconnect()
print("alice: left", flush=True)

The services are the ones of hello.py. Three things changed:

  • RtiAmbassador() replaces embedded.ambassador(), and connect_with_configuration gives the RTI address. The scheme of the address picks the transport: tcp://, tls://, ws:// or wss:// (see Python client).
  • Either federate may start first, so each creates the federation execution and ignores FederationExecutionAlreadyExists. On the way out each tries to destroy it and ignores FederatesCurrentlyJoined: whoever leaves last removes it.
  • Bob calls evoke_callback(1.0) (10.57), which waits up to one second for a callback and runs at most one. He stops after the text bye, or on Ctrl-C, and resigns properly in both cases.

An RTI ambassador also works in a with block (with RtiAmbassador() as alice:). At the end of the block, also when an exception leaves it, the ambassador calls close(): the connection ends, and a federate still joined is resigned by the RTI as a lost federate. The server then logs federate lost ... federate=alice. Resigning and disconnecting yourself, as these scripts do, remains the normal way out.

Run them

In a second terminal, start bob with the address the server printed:

python listener.py tcp://127.0.0.1:59144
bob: joined Greetings at tcp://127.0.0.1:59144, waiting

In a third terminal, alice says three lines:

python talker.py tcp://127.0.0.1:59144 "Hello, bob" "Nice weather on the bus today" bye
alice: said 'Hello, bob'
alice: said 'Nice weather on the bus today'
alice: said 'bye'
alice: left

Bob’s terminal now reads:

bob: joined Greetings at tcp://127.0.0.1:59144, waiting
bob: alice says 'Hello, bob'
bob: alice says 'Nice weather on the bus today'
bob: alice says 'bye'
bob: left

And the server logged the life of the federation execution (an extract):

2026-12-02T21:05:32.776187Z  INFO microrti::service: federation execution created session=961f509f22e995b4 federation=Greetings
2026-12-02T21:05:32.791336Z  INFO microrti::service: federate joined session=961f509f22e995b4 federation=Greetings federate=bob federate_type=greeter
2026-12-02T21:05:34.911420Z  INFO microrti::service: federate joined session=3057cdc082b9321a federation=Greetings federate=alice federate_type=greeter
2026-12-02T21:05:34.917816Z  INFO microrti::service: federate resigned session=3057cdc082b9321a federation=Greetings federate=alice federate_type=greeter
2026-12-02T21:05:34.921609Z  INFO microrti::service: federate resigned session=961f509f22e995b4 federation=Greetings federate=bob federate_type=greeter
2026-12-02T21:05:34.922119Z  INFO microrti::service: federation execution destroyed session=961f509f22e995b4 federation=Greetings

Bob has to start first. An interaction reaches the federates that are subscribed when it is sent, and the RTI keeps no copy for anyone who joins later. Data that a latecomer needs belongs in object instances: a federate that joins later discovers them and can ask their owners for the current attribute values.

The scripts ran unchanged over the other transports of the server (tls://, ws://, wss://); only the address differs.

When something goes wrong

What you seeWhat it means
ERROR: Package 'microrti' requires a different Python: 3.9.6 not in '>=3.11' from the pip install of the source archiveThe virtual environment was made with a Python older than 3.11. Delete .venv and make it again with a newer interpreter.
ERROR: microrti-0.3.0-cp311-abi3-manylinux_2_28_x86_64.whl is not a supported wheel on this platform.The Python of the virtual environment is older than 3.11, or the wheel does not match the machine: another processor, musl in place of glibc or the reverse, or a system that is not Linux.
microrti.ConnectionFailed: cannot reach the RTI: tcp://127.0.0.1:50097: transport error: cannot connect to tcp://127.0.0.1:50097: Connection refused (os error 61)Nothing listens at that address: the server is not running, or the port is not the one it printed. The error number depends on the operating system.
microrti.FederateNameAlreadyInUse: the federate name 'bob' is in use by federate 1Another bob is still joined, for example a listener from an earlier run. Stop it first.
Bob prints nothing after waitingAlice ran before bob had subscribed, or the two scripts were given different addresses.

Next steps