Lua subscriber to C++ api Publishers

4 posts / 0 new
Last post
Last seen: 5 years 1 week ago
Joined: 07/01/2014
Posts: 6
Lua subscriber to C++ api Publishers


My question :  is there a way for me to publish data using C++  API and to subscribe to that same data using the Lua prototype?

Background –

I am using RTI Connext , for my publishers set up I am making an IDL file that looks like the following



typedef long ArrayType[2];


module MyModule {

        struct PhoneCall2 {


        string<256> Caller;

        string<256> Message;

        long Time;

        long Member3[6];

        string<256> Diferent;

        ArrayType Member1[52];




And using Rrtiddsgen to generate the publisher C++ file.

After writing data to the types

 --- instance ->Member1[1][1] = 11;

and recompiling the publisher file  using the generated makefile, I am using the publisher executable I get to publish the data.


On the subscriber side I am using an xml file with the following type tag field



 <typedef name= 'ArrayType' type='long' arrayDimensions='2'/>


<module name="MyModule">

                <struct name ="PhoneCall2" >

                        <member name ="Caller" type ="string" key ="true" stringMaxLength= "256"/>

                        <member name ="Message" type ="string"  stringMaxLength= "256"/>

                        <member name ="Time" type ="long"/>

                        <member name ="Member3" type ="long"  arrayDimensions='6'/>

                        <member name ="Diferent" type ="string"  stringMaxLength= "256"/>

                        <member name ="Member1" type ="nonBasic"  nonBasicTypeName='ArrayType' arrayDimensions='52'/>




I am calling a lua file through the USER_QOS_PROFILE.xml  participant library tag , and through the Lua file I am reading and printing the data 

With this setup is it possible to have communications between this C++ publisher and Lua subscriber,  and if not is there a way to have a similar setup, where Lua based subscribers are used to read data from a C++ API publisher.?

gianpiero's picture
Last seen: 4 months 2 weeks ago
Joined: 06/02/2010
Posts: 174

Hello Kenny,

Yes: it is possible to publish in C++ and subscribe in Lua! With RTI Connext DDS you can pick many languages (C/C++/Java/Ada/Lua/C#) and all your application will communicate with each other seamlessly!!

Let’s go to your specific example. It is very easy and you did almost all the steps:

Publisher in C++:

  • take your idl and with rtiddsgen generate a c++ example:
rtiddsgen -example x64Darwin12clang4.1 test.idl
  • modify your test_publisher.cxx
/* Main loop */
for (count=0; (sample_count == 0) || (count < sample_count); ++count) {
    printf("Writing MyModule_PhoneCall2, count %d\n", count);
    /* Modify the data to be sent here */

    instance -&gt;Member1[1][1] = count; /This is what you want to add */

    retcode = MyModule_PhoneCall2_writer->write(*instance, instance_handle);
    if (retcode != DDS_RETCODE_OK) {
        printf("write error %d\n", retcode);
  • then compile your example:
gmake -f makefile_test_x64Darwin12clang4.1


Let's now move to the subscriber in Lua:

  • You can generate the xml type description from the idl using rtiddsgen:
 rtiddsgen -convertToXml test.idl

a test.xml file is created that contains your type definition.

  • Now you have to define in xml your domain/subscribe/topic and reader. For the type you can use the content of the xml we just generated. This is the complete file: quite simple but let me know if you have any question about it. 
  • Now the simple Lua code to read your type:


if not reader then
  reader = CONTAINER.READER["MySubscriber::MyReader"]
  if reader == nil then print("Error: reader is nil") end


for  i, sample in ipairs(reader.samples) do
  if (not reader.infos[i].valid_data) then
    print("\t invalid data!")
    print("\t Member1[2][2]=", sample['Member1[2][2]']) --note in Lua indexes start from 1 so if in C++ you are publishing Member1[1][1], in lua you have to read Member1[2][2]

Now, run!!!

  • In one terminal run your C++ publisher 
$ ./objs/x64Darwin12clang4.1/test_publisher
Writing MyModule_PhoneCall2, count 0
Writing MyModule_PhoneCall2, count 1
Writing MyModule_PhoneCall2, count 2
Writing MyModule_PhoneCall2, count 3
Writing MyModule_PhoneCall2, count 4
  • in the other terminal run the rtiddsprototyper like this
./rtiddsprototyper -cfgFile MySubscriber.xml -luaFile MySubscriber.lua
 RTI Connext Prototyper_with_Lua-Eng_build built with NDDS version: 5.1.0 (Core: 1.7a.00, C: 1.7a.00, C++: 1.7a.00)
 Copyright 2012 Real-Time Innovations, Inc.
  Member1[2][2]= 1
  Member1[2][2]= 2
  Member1[2][2]= 3
  Member1[2][2]= 4
 Finishing Prototyper..

Please let me know if this answer your question, 

   Gianpiero Napoli

Last seen: 5 years 1 week ago
Joined: 07/01/2014
Posts: 6

Thank you this worked out, but I do have another quick question.  I have 2 subscriber codes, One in C++ and one in Lua that will both read information from a Publisher in C++ code.  In my Subscriber in C++ I have the following code






I want to double check, for my second subscriber if the equivalent in XML is the following, and if there is anything else I would need to include in the xnl file or in the Lua code.



    <qos_library name="MyLibrary">

        <qos_profile name="DefaultProfile">














gianpiero's picture
Last seen: 4 months 2 weeks ago
Joined: 06/02/2010
Posts: 174

Hello Ken,

I am glad I could help. Now, for your next question: what you are doing in XML should be equivalent to what you are doing in C++. As described in the documentation here, if the add_peer() method is called before the DomainParticipant is enabled, the peer description will simply be added to the list that was populated by DDS_DiscoveryQosPolicy::initial_peers;

So what you are doing should be equivalent.

I would like to add, that even writing in C++ (or any other language) you can still use the XML file for QoS configuration! 

Let me know if you have more doubts.