MigratoryData Client API for C++
Developer's Guide and Reference Manual
MigratoryDataClient Class Reference

This class implements all the necessary operations for connecting to a cluster of one or more MigratoryData servers, subscribing to subjects, getting real-time messages for the subscribed subjects, and publishing real-time messages. More...

Public Member Functions

 MigratoryDataClient ()
 Create a MigratoryDataClient object.
 
void setLogListener (MigratoryDataLogListener *logListener)
 Define a log listener object. More...
 
void setLogLevel (MigratoryDataLogLevel logLevel)
 Configure the logging level. More...
 
void setEncryption (bool encryption)
 Configure whether to use SSL/TLS encryption when connecting to a MigratoryData server. More...
 
void setListener (MigratoryDataListener *listener)
 Attach a MigratoryDataListener for handling real-time messages and status notifications. More...
 
void setServers (std::vector< std::string > &servers)
 Specify a cluster of one or more MigratoryData servers to which the client will connect to. More...
 
void subscribe (std::vector< std::string > &subjects)
 Subscribe to one or more subjects. More...
 
void subscribeWithConflation (std::vector< std::string > &subjects, int conflationMillis)
 Subscribe to one or more subjects with conflation. More...
 
void unsubscribe (std::vector< std::string > &subjects)
 Unsubscribe from one or more subjects. More...
 
void setEntitlementToken (std::string &token)
 Assign an authorization token to the client. More...
 
void getSubjects (std::vector< std::string > &subjects)
 Return the list of subscribed subjects. More...
 
void notifyBeforeReconnectRetries (int n)
 Define the number of failed attempts to connect to one or more MigratoryData servers before triggering a status notification NOTIFY_SERVER_DOWN. More...
 
void notifyWhenReconnectRateExceedsThreshold (int n)
 Define the number of reconnect attempts to one or more MigratoryData servers per 3-minute window before triggering a status notification NOTIFY_RECONNECT_RATE_EXCEEDED. More...
 
void setQuickReconnectInitialDelay (int seconds)
 Define the number of seconds to wait before attempting to reconnect to the cluster after a connection failure is detected. More...
 
void setQuickReconnectMaxRetries (int retries)
 Define the maximum number of retries for the Quick Reconnect phase. More...
 
void setReconnectPolicy (std::string policy)
 Define the reconnect policy to be used after the Quick Reconnect phase. More...
 
void setReconnectTimeInterval (int seconds)
 Define the time interval used for the reconnect schedule after the Quick Reconnect phase. More...
 
void setReconnectMaxDelay (int seconds)
 Define the maximum reconnect delay for the TRUNCATED_EXPONENTIAL_BACKOFF policy. More...
 
void publish (MigratoryDataMessage &message)
 Publish a message. More...
 
void disconnect ()
 Disconnect from the connected MigratoryData server and dispose the resources used by the connection. More...
 
virtual ~MigratoryDataClient ()
 Destructor.
 

Public Attributes

const std::string NOTIFY_SERVER_UP
 Indicate that the client successfully connected to a MigratoryData server. More...
 
const std::string NOTIFY_SERVER_DOWN
 Indicate that the client failed to connect to a MigratoryData server. More...
 
const std::string NOTIFY_DATA_SYNC
 After a failover reconnection, the client synchronized a subscribed subject with the latest message available for that subject, as well as with all messages published during the failover for that subject. More...
 
const std::string NOTIFY_DATA_RESYNC
 After a failover reconnection, the client synchronized a subscribed subject with the latest message available for that subject, but not with the potential messages published during the failover, therefore behaving as a new client. More...
 
const std::string NOTIFY_SUBSCRIBE_ALLOW
 Indicate that the client was authorized to subscribe to a subject. More...
 
const std::string NOTIFY_SUBSCRIBE_DENY
 Indicate that the client was not authorized to subscribe to a subject. More...
 
const std::string NOTIFY_SUBSCRIBE_TIMEOUT
 
const std::string NOTIFY_PUBLISH_DENIED
 Indicate that the client was unable to publish a message because it is not allowed by your entitlement rules. More...
 
const std::string NOTIFY_PUBLISH_NO_SUBSCRIBER
 Indicate that the client was unable to publish a message because there is no client subscribed to the subject of the message. More...
 
const std::string NOTIFY_PUBLISH_OK
 Indicate that the client successfully published a message. More...
 
const std::string NOTIFY_PUBLISH_FAILED
 Indicate that the client was unable to publish a message. More...
 
const std::string NOTIFY_RECONNECT_RATE_EXCEEDED
 Indicate that the reconnect rate threshold per 3-minute window has been reached. More...
 
const std::string CONSTANT_WINDOW_BACKOFF
 A constant used to define the reconnect policy. More...
 
const std::string TRUNCATED_EXPONENTIAL_BACKOFF
 A constant used to define the reconnect policy. More...
 

Detailed Description

This class implements all the necessary operations for connecting to a cluster of one or more MigratoryData servers, subscribing to subjects, getting real-time messages for the subscribed subjects, and publishing real-time messages.

Member Function Documentation

◆ setLogListener()

void MigratoryDataClient::setLogListener ( MigratoryDataLogListener logListener)

Define a log listener object.

The log listener object should belong to a class which implements the MigratoryDataLogListener interface.

It is advisable to configure this API call first if you want to log as much as possible.

Parameters
logListenerAn instance of a class which implements the MigratoryDataLogListener interface

◆ setLogLevel()

void MigratoryDataClient::setLogLevel ( MigratoryDataLogLevel  logLevel)

Configure the logging level.

Parameters
logLevelThe logging verbosity (MigratoryDataLogLevel.LOG_ERROR, MigratoryDataLogLevel.LOG_INFO, MigratoryDataLogLevel.LOG_DEBUG or MigratoryDataLogLevel.LOG_TRACE ); by default MigratoryDataLogLevel.LOG_INFO is configured.

◆ setEncryption()

void MigratoryDataClient::setEncryption ( bool  encryption)

Configure whether to use SSL/TLS encryption when connecting to a MigratoryData server.

When using encryption you have to connect to the ports of the MigratoryData servers that are configured to listen for encrypted connections. See the parameter ListenEncrypted in the MigratoryData Configuration Guide

Parameters
encryptionDetermine whether the client connects to the MigratoryData server using an encrypted SSL/TLS connection

◆ setListener()

void MigratoryDataClient::setListener ( MigratoryDataListener listener)

Attach a MigratoryDataListener for handling real-time messages and status notifications.

Parameters
listenerAn instance of a class which implements the MigratoryDataListener interface

◆ setServers()

void MigratoryDataClient::setServers ( std::vector< std::string > &  servers)

Specify a cluster of one or more MigratoryData servers to which the client will connect to.

If you specify two or more MigratoryData servers, then all these MigratoryData servers should provide the same level of data redundancy, by making available for subscription the same set of subjects. This is required for achieving (weighted) load balancing, failover, and guaranteed message delivery of the system. In this way, the MigratoryData servers of the servers list form a cluster.

For example, to connect to a cluster formed of two MigratoryData servers installed at the addresses p1.example.com and p2.example.com, and configured to accept clients on the standard HTTP port 80, the following code can be used:

  vector<string> servers;
  servers.push_back("p1.example.com:80");
  servers.push_back("p2.example.com:80");
  client->setServers(servers);

To achieve load-balancing, the API connects the client to a MigratoryData server chosen randomly from the servers list. In this way, the load is balanced among all the members of the cluster.

Moreover, the API supports weighted load-balancing. This feature is especially useful if the MigratoryData servers in the cluster are installed on machines with different capacities. You can assign to each member of the cluster a weight ranging from 0 to 100. This weight assignment is a hint provided to the API to select with a higher probability a MigratoryData server with a higher weight either initially when the client connects to the cluster or later during a failover reconnection.

Supposing the address p1.example.com corresponds to a machine that is twice more powerful than the machine having the address p2.example.com, then you can assign to p1.example.com a weight 100 and to p2.example.com a weight 50 by prefixing each address with the assigned weight as follows:

 vector<string> servers;
  servers.push_back("100 p1.example.com:80");
  servers.push_back("50 p2.example.com:80");
  client->setServers(servers);

The API assigns a default weight 100 to the addresses not prefixed with a specific weight.

To achieve failover, if the connection between the client and a MigratoryData server is broken, then the API will automatically detect the failure and will select another MigratoryData server from the servers list. If the client fails to connect to the new selected server, a status notification NOTIFY_SERVER_DOWN will be triggered (unless you modify the number of failed attempts with MigratoryDataClient.notifyBeforeReconnectRetries()), and a new MigratoryData server in the cluster will be selected again and again until the client will be able to connect to one of the MigratoryData servers in the cluster. When successfully connected, the API will trigger a status notification NOTIFY_SERVER_UP.

Furthermore, if guaranteed message delivery is enabled, then the potential messages published for a subscribed subject during the failover period, will be automatically retrieved from the cache of the MigratoryData server to which the client reconnects to and a status notification NOTIFY_DATA_SYNC will be triggered for that subject.

If, for example, the failover period is abnormally long, and the client is not able to retrieve, after a failover reconnection, the messages published during the failover period for one of its subscribed subjects, then the API will retrieve only the most recent message available for that subject and will trigger a NOTIFY_DATA_RESYNC status notification for that subject, the client behaving as a new client which connects to the cluster at the moment of the failover reconnection.

For a complete discussion related to load balancing, failover, and guaranteed message delivery features see the MigratoryData Architecture Guide (PDF, HTML).

Parameters
serversAn array of strings where each string represents the network address (IP address or DNS domain name and its corresponding port) of a MigratoryData server, optionally prefixed by a weight ranging from 0 to 100. If the weight prefix is not provided to an address, then the API will automatically assign to that address a default weight 100.

◆ subscribe()

void MigratoryDataClient::subscribe ( std::vector< std::string > &  subjects)

Subscribe to one or more subjects.

Subscribe for real-time messages having as subjects the strings provided in the subjects parameter.

As an example, supposing messages are market data updates having as subjects stock names. Then, to subscribe for the messages having as subjects /stocks/NYSE/IBM and /stocks/Nasdaq/MSFT the following code will be used:

    vector<string> subjects;
    subjects.push_back("/stocks/NYSE/IBM");
    subjects.push_back("/stocks/Nasdaq/MSFT");
    client->subscribe(subjects);

The subjects are strings having a particular syntax. See the Chapter "Concepts" in the MigratoryData Architecture Guide (PDF, HTML) to learn about the syntax of the subjects.

Parameters
subjectsAn array of strings representing subjects.

◆ subscribeWithConflation()

void MigratoryDataClient::subscribeWithConflation ( std::vector< std::string > &  subjects,
int  conflationMillis 
)

Subscribe to one or more subjects with conflation.

Subscribe for real-time messages having as subjects the strings provided in the subjects parameter.

If the optional parameter conflationMillis is used, then for each subject in the subjects list given in argument, its messages will be aggregated in the MigratoryData server and published every conflationMillis milliseconds as aggregated data (containing only the latest value for that subject and its latest field values). The value of conflationMillis should be a multiple of 100 milliseconds, otherwise the MigratoryData server will round it to the nearest value multiple of 100 milliseconds (e.g. 76 will be rounded to 0, 130 will be rounded to 100, 789 will be rounded to 700, ...). If the value of conflationMillis is 0 (or is rounded to 0), then no conflation will apply, and data publication will be message-by-message with no message aggregation.

As an example, supposing the messages are market data updates having as subjects stock names. Then, to subscribe for the messages having as subjects /stocks/NYSE/IBM and /stocks/Nasdaq/MSFT using 1-second conflation the following code will be used:

    vector<string> subjects;
    subjects.push_back("/stocks/NYSE/IBM");
    subjects.push_back("/stocks/Nasdaq/MSFT");
    client->subscribeWithConflation(subjects, 1000);

The subjects are strings having a particular particular syntax. See the Chapter "Concepts" in the MigratoryData Architecture Guide (PDF, HTML) to learn about the syntax of the subjects.

Parameters
subjectsAn array of strings representing subjects.
conflationMillisAn optional argument defining the number of milliseconds used to aggregate ("conflate") the messages for each subject in the subjects list; default value is 0 meaning that no conflation will apply, and data publication will be message-by-message with no message aggregation.

◆ unsubscribe()

void MigratoryDataClient::unsubscribe ( std::vector< std::string > &  subjects)

Unsubscribe from one or more subjects.

Unsubscribe from the subscribed subjects provided in the subjects parameter.

Parameters
subjectsAn array of strings representing subjects.

◆ setEntitlementToken()

void MigratoryDataClient::setEntitlementToken ( std::string &  token)

Assign an authorization token to the client.

To define which users of your application have access to which subjects, you will first have to set the parameter Entitlement in the configuration file of the MigratoryData server, see the parameter Entitlement in the MigratoryData Configuration Guide (PDF, HTML).

Then, you will have to use the entitlement-related part of the MigratoryData Extension API to allow or deny certain users to subscribe / publish to certain subjects.

Parameters
tokenA string representing an authorization token.

◆ getSubjects()

void MigratoryDataClient::getSubjects ( std::vector< std::string > &  subjects)

Return the list of subscribed subjects.

Parameters
[out]subjectsA vector of strings representing the subscribed subjects.

◆ notifyBeforeReconnectRetries()

void MigratoryDataClient::notifyBeforeReconnectRetries ( int  n)

Define the number of failed attempts to connect to one or more MigratoryData servers before triggering a status notification NOTIFY_SERVER_DOWN.

Parameters
nThe number of the failed attempts to connect to one or more MigratoryData servers before triggering a status notification NOTIFY_SERVER_DOWN; default value is 1.

◆ notifyWhenReconnectRateExceedsThreshold()

void MigratoryDataClient::notifyWhenReconnectRateExceedsThreshold ( int  n)

Define the number of reconnect attempts to one or more MigratoryData servers per 3-minute window before triggering a status notification NOTIFY_RECONNECT_RATE_EXCEEDED.

Parameters
nThe number of reconnect attempts to one or more MigratoryData servers per 3-minute window before triggering a status notification NOTIFY_RECONNECT_RATE_EXCEEDED; default value is 15.

◆ setQuickReconnectInitialDelay()

void MigratoryDataClient::setQuickReconnectInitialDelay ( int  seconds)

Define the number of seconds to wait before attempting to reconnect to the cluster after a connection failure is detected.

Connection Failure Detection

Connection failure is detected immediately for almost all users running modern browsers. For a few users running modern browsers (and being subject to temporary, atypical network conditions) as well as for all users running older browsers without WebSocket support, connection failure is detected after 30-40 seconds.

Reconnection Phases and Policies

When a connection failure is detected, the API will attempt to reconnect to the servers of the MigratoryData cluster as follows: First, it will attempt to reconnect (up to a number of times as defined by MigratoryDataClient.setQuickReconnectMaxRetries()) using small delays between retries (the Quick Reconnection phase). If the connection cannot be established after the Quick Reconnection phase, then the API will attempt to reconnect less frequently according to the policy defined by MigratoryDataClient.setReconnectPolicy().

The delays between retries are computed acoording to the following algoritm where the values of the variables involved are defined by the API methods having substantially the same names:

   Quick Reconnect Phase (retries <= quickReconnectMaxRetries)
   -----------------------------------------------------------
      (retries starts with 1 and increment by 1 at each quick reconnect)
      reconnectDelay = quickReconnectInitialDelay * retries - random(0, quickReconnectInitialDelay)
   After Quick Reconnect Phase (retries > quickReconnectMaxRetries)
   ----------------------------------------------------------------
      (reset retries to start with 1 and increment by 1 at each reconnect)
      If reconnectPolicy is CONSTANT_WINDOW_BACKOFF, then
         reconnectDelay = reconnectTimeInterval
      else if reconnectPolicy is TRUNCATED_EXPONENTIAL_BACKOFF, then
         reconnectDelay = min(reconnectTimeInterval * (2 ^ retries) - random(0, reconnectTimeInterval * retries), reconnectMaxDelay)

For a few users running modern browsers (and being subject to temporary, atypical network conditions) as well as for all users running older browsers without WebSocket support, if reconnectDelay computed with the algorithm above is less than 10 seconds, then it is rounded to 10 seconds.

Parameters
secondsThe number of seconds to wait before attempting to reconnect to the cluster; default value is 5 seconds.

◆ setQuickReconnectMaxRetries()

void MigratoryDataClient::setQuickReconnectMaxRetries ( int  retries)

Define the maximum number of retries for the Quick Reconnect phase.

Parameters
retriesThe maximum number of quick reconnect retries; default value is 3.

◆ setReconnectPolicy()

void MigratoryDataClient::setReconnectPolicy ( std::string  policy)

Define the reconnect policy to be used after the Quick Reconnect phase.

See MigratoryDataClient.setQuickReconnectInitialDelay() to learn about the Quick Reconnect phase and the reconnect schedule for the policy defined by this method.

Parameters
policyThe reconnect policy to be used after the Quick Reconnect phase. The possible values are CONSTANT_WINDOW_BACKOFF and TRUNCATED_EXPONENTIAL_BACKOFF; the default value is TRUNCATED_EXPONENTIAL_BACKOFF.

◆ setReconnectTimeInterval()

void MigratoryDataClient::setReconnectTimeInterval ( int  seconds)

Define the time interval used for the reconnect schedule after the Quick Reconnect phase.

See MigratoryDataClient.setQuickReconnectInitialDelay() to learn about the Quick Reconnect phase and how the value defined by this API method is used.

Parameters
secondsA time interval expressed in seconds; default is 20 seconds.

◆ setReconnectMaxDelay()

void MigratoryDataClient::setReconnectMaxDelay ( int  seconds)

Define the maximum reconnect delay for the TRUNCATED_EXPONENTIAL_BACKOFF policy.

See MigratoryDataClient.setQuickReconnectInitialDelay() to learn how the value defined by this API method is used.

Parameters
secondsThe maximum reconnect delay when the policy TRUNCATED_EXPONENTIAL_BACKOFF is used; default value is 360 seconds.

◆ publish()

void MigratoryDataClient::publish ( MigratoryDataMessage message)

Publish a message.

If the message includes a closure data, then a status notification will be provided via MigratoryDataListener.onStatus() to inform whether the message publication has been successful or not.

Parameters
messageA MigratoryDataMessage message

◆ disconnect()

void MigratoryDataClient::disconnect ( )

Disconnect from the connected MigratoryData server and dispose the resources used by the connection.

This method should be called when the connection is no longer necessary.

Member Data Documentation

◆ NOTIFY_SERVER_UP

const std::string MigratoryDataClient::NOTIFY_SERVER_UP

Indicate that the client successfully connected to a MigratoryData server.

This constant indicates that the client successfully connected to one of the MigratoryData servers defined with the API method MigratoryDataClient.setServers().

◆ NOTIFY_SERVER_DOWN

const std::string MigratoryDataClient::NOTIFY_SERVER_DOWN

Indicate that the client failed to connect to a MigratoryData server.

This constant indicates that the client failed to connect to one of the MigratoryData servers defined with the API method MigratoryDataClient.setServers().

◆ NOTIFY_DATA_SYNC

const std::string MigratoryDataClient::NOTIFY_DATA_SYNC

After a failover reconnection, the client synchronized a subscribed subject with the latest message available for that subject, as well as with all messages published during the failover for that subject.

This constant indicates that the client successfully synchronized the subject provided in the detail information of the status notification. Also, the potential messages published for that subject during the failover period have been successfully retrieved at the moment of the reconnection.

◆ NOTIFY_DATA_RESYNC

const std::string MigratoryDataClient::NOTIFY_DATA_RESYNC

After a failover reconnection, the client synchronized a subscribed subject with the latest message available for that subject, but not with the potential messages published during the failover, therefore behaving as a new client.

This constant indicates that the client successfully synchronized the subject provided in the detail information of the status notification. However, the client was unable to get the messages published during the failover. Therefore, it behaves like a new client which connects to the MigratoryData server at the moment of the failover reconnection.

◆ NOTIFY_SUBSCRIBE_ALLOW

const std::string MigratoryDataClient::NOTIFY_SUBSCRIBE_ALLOW

Indicate that the client was authorized to subscribe to a subject.

This constant indicates that the client – identified with the token defined with the API method MigratoryDataClient.setEntitlementToken() – is allowed to subscribe to the subject provided in the detail information of the status notification.

◆ NOTIFY_SUBSCRIBE_DENY

const std::string MigratoryDataClient::NOTIFY_SUBSCRIBE_DENY

Indicate that the client was not authorized to subscribe to a subject.

This constant indicates that the client – identified with the token defined with the API method MigratoryDataClient.setEntitlementToken() – is not allowed to subscribe to the subject provided in the detail information of the status notification.

◆ NOTIFY_PUBLISH_DENIED

const std::string MigratoryDataClient::NOTIFY_PUBLISH_DENIED

Indicate that the client was unable to publish a message because it is not allowed by your entitlement rules.

This constant is used to indicate that the publication of the message, having the closure provided in the detail information of the status notification, has failed. The publication failed because the client – identified with the token defined with the API method MigratoryDataClient.setEntitlementToken() – is not allowed to publish on the subject of the message.

◆ NOTIFY_PUBLISH_NO_SUBSCRIBER

const std::string MigratoryDataClient::NOTIFY_PUBLISH_NO_SUBSCRIBER

Indicate that the client was unable to publish a message because there is no client subscribed to the subject of the message.

This constant is used to indicate that the publication of the message, having the closure provided in the detail information of the status notification, has failed. The publication failed because there is no client then subscribed to the subject of the message.

Deprecated:
no more is use.

◆ NOTIFY_PUBLISH_OK

const std::string MigratoryDataClient::NOTIFY_PUBLISH_OK

Indicate that the client successfully published a message.

This constant is used to indicate that the publication of the message, having the closure provided in the detail information of the status notification, has succeeded.

◆ NOTIFY_PUBLISH_FAILED

const std::string MigratoryDataClient::NOTIFY_PUBLISH_FAILED

Indicate that the client was unable to publish a message.

This constant is used to indicate that the publication of the message, having the closure provided in the detail information of the status notification, has failed.

◆ NOTIFY_RECONNECT_RATE_EXCEEDED

const std::string MigratoryDataClient::NOTIFY_RECONNECT_RATE_EXCEEDED

Indicate that the reconnect rate threshold per 3-minute window has been reached.

This constant is used to indicate that during the last 3 minutes, the reconnect rate threshold defined by MigratoryDataClient.notifyWhenReconnectRateExceedsThreshold() has been reached.

◆ CONSTANT_WINDOW_BACKOFF

const std::string MigratoryDataClient::CONSTANT_WINDOW_BACKOFF

A constant used to define the reconnect policy.

See MigratoryDataClient.setQuickReconnectInitialDelay() for more details about this policy.

◆ TRUNCATED_EXPONENTIAL_BACKOFF

const std::string MigratoryDataClient::TRUNCATED_EXPONENTIAL_BACKOFF

A constant used to define the reconnect policy.

See MigratoryDataClient.setQuickReconnectInitialDelay() for more details about this policy.