C++ Client API

powerful. fast. easy.

Building a high-performance messaging application in C++? Use AMPS along with the AMPS C/C++ Client to build robust, blazing fast applications for Windows and Linux. Here's a few examples to get you started:

Example 1: Connect and Subscribe

Connecting and subscribing to a content-filtered topic couldn't be easier:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <amps.h>
#include <iostream>
    ...
    int main()
    {
      AMPS::Client amps("myapp");

      try
      {

        amps.connect("tcp://localhost:9007/amps/json");
        amps.logon();

        for (auto msg : amps.subscribe("orders",
                                       "/symbol LIKE 'ABCD'"))
        {

           std::cout << msg.getData() << std::endl;

        }

      } catch (AMPSException &e) {
        std::cerr << e.what() << std::endl;
        return -1;
      }
      return 0;
    }

In this example, we connect to AMPS and subscribe to the "orders" topic. Messages are filtered on the server: we're only sent messages whose "symbol" field contains "ABCD". As messages arrive, the program prints messsage data to the console. If any errors occur while connecting or subscribing, they're printed to the console as well.

Example 2: Automatic Reconnection and Resubscription

Rock-solid applications must be able to recover from network outages. The AMPS C++ client includes an HAClient class that includes automatic reconnection and resubscription. Best of all, easy-to-implement interfaces control reconnection and resubscription behavior, allowing you to easily customize failover.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
AMPS::HAClient client("reconnectingSubscriber");

// The ServerChooser interface tells the HAClient which server
// to connect to, both for the initial connection and failover.
// The DefaultServerChooser is included with the client: many
// applications implement a ServerChooser to control failover
// behavior.


AMPS::ServerChooser chooser(new AMPS::DefaultServerChooser());
chooser.add("tcp://amps-server:9007/amps/json");
chooser.add("tcp://amps-failover-server:9007/amps/json");
 
client.setServerChooser(chooser);
client.connectAndLogon();


// Enter subscriptions. If the connection to the server
// is lost, the HAClient will restore the connection to
// the server or the failover partner, and restore
// subscriptions.
client.subscribe(AMPS::MessageHandler(handler_function, NULL),
                 "my-cool-topic");

The HAClient class can, optionally, also provide store-and-forward for reliable publish. With the AMPS transaction log, the class can provide resumable subscriptions that are guaranteed not to miss messages or receive duplicate messages, even in the case of failover between replicated servers. See the C++ Developer Guide for more information on enabling these capabilities of the HAClient (and other options for the class).

Also, notice that message handlers can be specified using C++ function objects (including lambda functions) for type-safety, or using raw function pointers as in this example.

Example 3: Query the Contents of a "SOW" Topic

State-of-the-World ("SOW") topics in AMPS combine the power of a database table with the performance of a publish-subscribe system. Use the AMPS C/C++ Client to query the contents of a SOW topic, and retrieve ongoing updates:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class MyApp {
      std::map<std::string, std::string> _data;
      AMPS::Client _client;

      void handleMessage(const Message& m) {
        if(m.getCommand()=="oof") {
          _data.remove(m.getSowKey());
        } else if(m.getCommand()=="sow" ||
                  m.getCommand()=="publish") {
          _data[m.getSowKey()] = m.getData();
        }
      }

      public:
      void run() {

        auto messageHandler = [&](const AMPS::Message& m)
                                  { handleMessage(m); } ;

        _client.executeAsync(
                         Command("sow_and_subscribe").
                             setTopic("orders").
                             setFilter("/symbol LIKE 'ABC' "
                                       "AND /qty > 100").
                             setOptions("oof"),
                         messageHandler);
                 );
      }
    }

In this example, we keep a local std::map up to date with the changes in an AMPS topic. Our message handler is called once for each row in the initial result set for our query of "orders." After that, it continues to be called each time any attribute of the order changes, with the Command set to "publish." When an order is no longer present in the orders topic, our handler is called with a message that has a Command of "oof."

Notice that, in this example, all of the processing occurs on a background thread. Many high performance applications receive messages on one thread, and enqueue those messages to be processed by worker threads. The AMPS C++ client includes an asynchronous interface that makes it easy to build high-performance multithreaded programs that follow this pattern.

Everything You Need

Ready to learn more? The AMPS C/C++ Client includes everything you need to get started: libraries, sample applications, developer's guide, and reference documentation. Each AMPS Client includes complete source code so you can fully learn and understand AMPS, and best integrate it into your environment.

Click here (Linux) or here (Windows) to download and start using the AMPS C/C++ Client. Once you've downloaded it, read the Developer Guide to get started.

The current release of the C++ client is version 5.3.4.4, which supports AMPS servers version 5.3 and earlier.