Getting Started with Messenger

The Proton Messenger interface lets you get started sending and receiving messages easily.

Sending a Single Message

We create a message, then set its address and content. We create and start a messenger, put the message, and send it.
Finally, we stop the messenger.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>

    #include "proton/message.h"
    #include "proton/messenger.h"


    int
    main(int argc, char** argv)
    {
      int c;
      char * msgtext = "Hello, Proton!";
      pn_message_t   * message;
      pn_messenger_t * messenger;
      pn_data_t      * body;


      message = pn_message();
      pn_message_set_address(message, "amqp://0.0.0.0:6666");
      body = pn_message_body(message);
      pn_data_put_string(body, pn_bytes(strlen(msgtext), msgtext));

      messenger = pn_messenger(NULL);
      pn_messenger_start(messenger);

      pn_messenger_put(messenger, message);
      pn_messenger_send(messenger);

      pn_messenger_stop(messenger);
      pn_messenger_free(messenger);

      return 0;
    }

Receiving a Single Message

We create a message that we will use to receive to. We create and start a messenger, then subscribe to our address. Please note the use of the tilde at the beginning of the incoming address. We receive a single message to our incoming queue, and then dequeue it into the message structure we created earlier. Print out the data, and stop the messenger.

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>

    #include "proton/message.h"
    #include "proton/messenger.h"



    #define BUFSIZE 1024


    int
    main(int argc, char** argv)
    {
      size_t bufsize = BUFSIZE;
      char buffer[BUFSIZE];
      pn_message_t   * message;
      pn_messenger_t * messenger;
      pn_data_t      * body;


      message = pn_message();

      messenger = pn_messenger(NULL);
      pn_messenger_start(messenger);
      pn_messenger_subscribe(messenger, "amqp://~0.0.0.0:6666");

      pn_messenger_recv(messenger, 1);
      pn_messenger_get(messenger, message);

      body = pn_message_body(message);
      pn_data_format(body, buffer, & bufsize);
      printf("Received: |%s|\n", buffer);

      pn_messenger_stop(messenger);

      return 0;
    }

Would You Like to Know More?