RTI Connext C# API  6.1.0
Publication examples

How to publish a topic and work with Publishers and DataWriters.

How to publish a topic and work with Publishers and DataWriters.


See also Entity examples for examples that affect all DDS Entities, including Publishers and DataWriters.

Writing data and instances

Example: create a DataWriter using default QoS and publish a data sample

// Create a DomainParticipant on domain 0
using Rti.Dds.Domain.DomainParticipant participant =
factory.CreateParticipant(domainId: 0);
// Create a topic called "Square" for the type Shape
Rti.Dds.Topics.Topic<Shape> topic =
// In many cases there is no need to explicitly create a Publisher;
// the ImplicitPublisher can be used to create the DataWriter.
Rti.Dds.Publication.DataWriter<Shape> writer =
// Create a data sample
var shape = new Shape
color = "GREEN",
shapesize = 50,
x = 0,
y = 20
Singleton that manages the creation of DomainParticipant objects.
Definition: DomainParticipantFactory.cs:25
static DomainParticipantFactory? Instance
Gets the singleton instance of this class.
Definition: DomainParticipantFactory.cs:39
Container for all other Entity objects.
Definition: DomainParticipant.cs:39
Allows an application to set the value of the data to be published under a given Topic<T>.
Definition: DataWriter.cs:29
void Write(T sample)
Modifies the value of a data instance.
The most basic description of the data to be published and subscribed.
Definition: Topic.cs:33
Contains DomainParticipant and related classes.
Definition: DomainParticipant.cs:33
Contains the classes to support publishing topics.
Definition: Namespaces.cs:74
Contains classes for defining topics.
Definition: Namespaces.cs:67
Contains the RTI Connext DDS C# API.
Definition: AsyncWaitSetProperty.cs:18
Contains the RTI Connext C# API.
Definition: Logger.cs:20

The IDL definition of the Shape type used in the example is the following:

module Example {
struct Shape {
@key string<128> color;
int32 x;
int32 y;
int32 shapesize;
Definition: MyType.cs:17

For more information about IDL types, see IDL examples.

Example: registering and disposing an instance

// When writing the same instance multiple times, register it first
// and then pass the instance handle to Write. This makes the operation
// more efficient.
var greenHandle = writer.RegisterInstance(new Shape { color = "GREEN" });
var shape = new Shape
color = "GREEN",
shapesize = 50,
x = 0,
y = 20
for (int i = 0; i < 100; i++)
shape.x = i;
writer.Write(shape, greenHandle);
// To indicate that the instance is no longer part of the system,
// call DisposeInstance. A status update will be received as part of
// sample.Info.State.Instance on the reader side.

Example: writing data with additional parameters

var greenHandle = writer.RegisterInstance(new Shape { color = "GREEN" });
var shape = new Shape
color = "GREEN",
shapesize = 50,
x = 0,
y = 20
// Write a sample with additional parameters:
writer.Write(shape, new WriteParams
// Set the instance handle to make the write operation faster
InstanceHandle = greenHandle,
// Set an arbitrary timestamp. The reader will see it in
// sample.Info.SourceTimestamp.
SourceTimestamp = Time.FromSeconds(1),
// Send an arbitrary integer flag. The reader will see it in
// sample.Info.Flag. This can be used to attach some meta-data to
// each sample without changing the type definition.
Flag = 1234
// ...

These examples from the rticonnextdds-examples GitHub repository show other advanced ways to write data:

Instance management is discussed in the Keys and Instances section of the Getting Started Guide, which explains how to use the following methods:

Configuring a Publisher

Example: create a publisher with a Partition QoS

// Create a explicit Publisher with a Partition QoS
var publisherQos = participant.DefaultPublisherQos
.WithPartition(p => p.Add("A"));
Publisher publisher = participant.CreatePublisher(publisherQos);
// All the DataWriters created from this Publisher will communicate
// only with DataReaders in a matching partition.
var writer1 = publisher.CreateDataWriter(topic1);
var writer2 = publisher.CreateDataWriter(topic2);

These examples from the rticonnextdds-examples GitHub repository demonstrate uses cases that involve a Publisher:

Looking up a previously created Entity

Example: lookup a previously-created DataWriter

// Create a publisher and two writers. Each entity can optionally have
// an arbitrary name assigned as part of its QoS.
var publisher = participant.CreatePublisher(
DataWriter<Shape> writer1 = publisher.CreateDataWriter(topic1);
DataWriter<Shape> writer2 = publisher.CreateDataWriter(
// ...
// Look up the previously created writers by their topic name
AnyDataWriter lookupWriter1 = publisher.LookupDataWriter(topic1.Name);
Debug.Assert(writer1 == (DataWriter<Shape>) lookupWriter1);
AnyDataWriter lookupWriter2 = publisher.LookupDataWriter(topic2.Name);
Debug.Assert(writer2 == (DataWriter<Shape>) lookupWriter2);
// Look up a writer by entity name (only if a name was assigned in the QoS)
lookupWriter2 = publisher.LookupDataWriterByName("MyWriter2");
Debug.Assert(writer2 == (DataWriter<Shape>) lookupWriter2);
lookupWriter2 = participant.LookupDataWriter("MyPublisher::MyWriter2");
Debug.Assert(writer2 == (DataWriter<Shape>) lookupWriter2);
// Access all writers
foreach (AnyDataWriter writer in publisher.DataWriters)
IAnyTopic relatedTopic = writer.TopicUntyped;
Console.WriteLine($"DataWriter for topic {relatedTopic.Name} and type {relatedTopic.TypeName}");
// Access all writers in all publishers
foreach (Publisher pub in participant.Publishers)
string pubName = pub.Qos.PublisherName.Name ?? "(unnamed)";
Console.WriteLine($"Publisher: {pubName}");
foreach (AnyDataWriter writer in pub.DataWriters)
IAnyTopic relatedTopic = writer.TopicUntyped;
Console.WriteLine($"- DataWriter for topic {relatedTopic.Name} and type {relatedTopic.TypeName}");
@ Debug
The message contains debug information that might be relevant to your application.

Looking up a matched subscription

Example: lookup matched subscriptions

// Look up all the subscriptions:
foreach (var subscription in writer.GetMatchedSubscriptionData())
// subscription name is available if the reader was created with
// DataReaderQos.WithSubscriptionName
string name = subscription.SubscriptionName.Name;
string typeName = subscription.TypeName;
// ...

See also Looking up matched publications and Entity Discovery.