Regular Expression Subscriptions

Regular Expression (Regex) subscriptions allow a regular expression to be supplied in the place of a topic name. When you supply a regular expression, it is as if a subscription is made to every topic that matches your expression, including topics that do not yet exist at the time of creating the subscription.

To use a regular expression, simply supply the regular expression in place of the topic name in the subscribe command. For example:


std::string subscriptionId = client.executeAsync(
              Command("subscribe").setTopic("orders.*"),
              MessageHandler(myHandlerFunction, NULL));

...

/* The myHandlerFunction is a global function that is invoked by the AMPS client
 * whenever a matching message is received. The first parameter, message, is 
 * a reference to an AMPS Message object that contains the data and headers
 * of the received message. The second parameter, userData, is set to whatever
 * value was provided in the MessageHandler constructor -- NULL in this example.
 */
void myHandlerFunction(const Message& message, void* userData)
{
    std::cout << message.getTopic() << ": " << message.getData() << std::endl;
}

In this example, messages on topics orders-north-america, orders-europe, and new-orders would match the regular expression. Messages published to any of those topics will be sent to our message_handler function. As in the example, you can use the getTopic() function to determine the actual topic of the message sent to the function.

Last updated