Hello WorldΒΆ

This section shows the basic code to publish and subscribe to a topic defined by a simple data type. Similar code can be generated by rtiddsgen from an IDL or XML file defining your data types. The sections that follow expand on the concepts shown in this example.

Define your types:

# hello.py

import rti.types as idl

@idl.struct
class HelloWorld:
    message: str = ""

Create a DataWriter to publish the HelloWorld Topic:

# hello_publisher.py

import time
import rti.connextdds as dds
from hello import HelloWorld

participant = dds.DomainParticipant(domain_id=0)
topic = dds.Topic(participant, 'HelloWorld Topic', HelloWorld)
writer = dds.DataWriter(participant.implicit_publisher, topic)

for i in range(10):
    writer.write(HelloWorld(message=f'Hello World! #{i}'))
    time.sleep(1)

Create a DataReader to subscribe to the HelloWorld Topic:

 # hello_subscriber.py

 import rti.connextdds as dds
 import rti.asyncio
 from hello import HelloWorld

 participant = dds.DomainParticipant(domain_id=0)
 topic = dds.Topic(participant, 'HelloWorld Topic', HelloWorld)
 reader = dds.DataReader(participant.implicit_subscriber, topic)

 async def print_data():
     async for data in reader.take_data_async():
         print(f"Received: {data}")

rti.asyncio.run(print_data())

Continue to the next sections to learn about a DomainParticipant, Topics, Publications, Subscriptions, and Data Types.