The "subscribeToMq" action returns the retained message of each successfully subscribed topic. The server processes the retained message using the default "variantFormat" value, which is "json". This can be a problem if the retained message contains non-JSON data. To ensure your application can process binary payloads in messages, add "variantFormat": "binary" and "binaryFormat": "base64" or "binaryFormat": "hex" to the "responseOptions" property of the "subscribeToMq" action.
Your application must retrieve a retained message from the response to the "subscribeToMq" action because it is the only way to retrieve it when using the MQ actions. A subsequent call to the "getMessagesFromMqApiSession" action will not return the retained message.
You can use the "subscribeToMq" action to manage the subscriptions of any session without disconnecting an MQTT client as long as your account has permission to do so. Thus, you can centrally manage the subscriptions for MQTT and MQ API sessions.
Use the required "clientName" property to identify the client's session. An MQTT or MQ API session with that client identifier must already exist; otherwise, the action returns an error. The FairCom server tracks session statistics so you can monitor the health of a client and its session, including the timestamp of the last received message, the client's subscription receive rate, the topics the session subscribes to, etc.
The "subscriptionActions" property contains a list of subscription objects that can add or remove a topic filter from a client's subscriptions.
- Use the required
"subscriptionAction"property to add or remove a topic filter:"add"adds a topic filter to the subscription and"remove"removes it. - Use the required
"topicFilter"property to specify a wildcard topic to subscribe to multiple topics. - Use the optional
"subscriptionIdentifier"property to assign a unique identifier to the subscription. When the FairCom server sends an MQTT 5 message to the client, it includes the subscription identifier to help the client identify the subscription quickly and easily. - Use the optional
"includeExpiredMessages": trueto include expired messages in the subscription. It defaults tofalse. - Use the optional
"receiveRetainedMessages"property to control when the server sends retained messages to your subscription, which includes never sending the retained message, sending it only when the client subscribes for the first time, or sending it each time the client subscribes. - Use the optional
"receiveMyMessages"property to prevent or allow the server to send messages to a client that the client published.
Tip Use
"subscribeToMq"to configure subscriptions for any MQ API and MQTT client. You can set MQTT 5 subscription properties for an MQTT 3.1.1 client. You can centrally manage subscriptions for clients.
Note The account logged into the MQ API must have permission to access the session identified by
"clientName".
Request example
Minimal
{
"action": "subscribeToMq",
"params": {
"clientName": "client1",
"subscriptionActions": [
{
"subscriptionAction": "add",
"topicFilter": "test/topic1"
}
]
},
"authToken": "replaceWithAuthTokenFromCreateSession",
"requestId": "00000001"
}Maximal
{
"apiVersion": "1.0",
"api": "mq",
"action": "subscribeToMq",
"params": {
"clientName": "client1",
"subscriptionActions": [
{
"subscriptionAction": "add",
"topicFilter": "myTopic",
"qos": 0,
"subscriptionIdentifier": 268435455,
"binaryFormat": "base64",
"includeExpiredMessages": true,
"receiveMyMessages": false,
"receiveRetainedMessages": "onSubscription",
"receiveRetainedFlagAsPublished": true,
"userProperties": [
{
"key": "some key",
"value": "some value"
}
]
},
{
"subscriptionAction": "add",
"topicFilter": "some/topic2/name/#",
"userProperties": [
{
"key": "some key",
"value": "some value"
}
]
}
]
},
"authToken": "replaceWithAuthTokenFromCreateSession",
"requestId": "00000001",
"debug": "max"
}
Response example
Success
Notice that the subscription object’s "errorCode" and "errorMessage" properties include the error information, but the response’s the subscription object’s "errorCode" and "errorMessage" properties do not.
{
"authToken": "authToken",
"result": {
"subscriptions": [
{
"matchingTopics": [
],
"topicFilter": "Test/Topic1",
"errorCode": 0,
"errorMessage": "",
"debugInfo": {
}
},
{
"matchingTopics": [
],
"topicFilter": "Test/Topic2",
"errorCode": 0,
"errorMessage": "",
"debugInfo": {
}
}
]
},
"requestId": "5",
}
Failure
When using the "subscribeToMq" action to subscribe to one or more topics, a failure of one subscription does not set the "errorCode" to a failure value. This is because some topics may be successfully subscribed to and some may not. The caller must check the "subscriptions" property in the response. It is an array of subscription objects and each object contains the status of each topic in the subscription request. If a topic has an error, the subscription object’s "errorCode" and "errorMessage" properties include the error information.
Notice that the subscription object’s "errorCode" and "errorMessage" properties include the error information, but the response’s the subscription object’s "errorCode" and "errorMessage" properties do not.
{
"authToken": "authToken",
"result": {
"subscriptions": [
{
"matchingTopics": [
"myTopic"
],
"topicFilter": "myTopic",
"retainedMessages": [
{}
],
"errorCode": 12012,
"errorMessage": "Not able to subscribe - 12012 - Invalid JSON payload. Please retry with [variantformat] of [variantObject] to identify the correct message payload format.",
"debugInfo": {}
}
]
},
"errorCode": 0,
"errorMessage": ""
}
Properties
Request properties ("params")
| Property | Description | Default | Type | Limits (inclusive) |
|---|---|---|---|---|
clientName |
The "clientName" property specifies the unique name that identifies the client to the FairCom MQ engine. |
Optional with default of ""
|
string | 0 to 65,550 bytes |
subscriptionActions |
The
|
Optional with default of []
|
array |
|
|
subscriptionActions .binaryFormat |
The "binaryFormat" property designates the format of binary values embedded in JSON strings. For more details, see "binaryFormat". |
Optional with default of "hex"
|
string | One of the following: "base64", "hex", or "byteArray". |
|
subscriptionActions .includeExpiredMessages |
The
|
Optional with default of true
|
Boolean |
|
|
subscriptionActions .qos |
The "qos" property defines the MQTT quality of service for publishing the message that will be sent to the MQTT broker. |
Optional with default of 1
|
enumerated integer |
|
|
subscriptionActions .receiveMyMessages |
The |
Optional with default of true
|
Boolean |
|
|
subscriptionActions .receiveRetainedFlagAsPublished |
The When When |
Optional with default of true
|
Boolean |
|
|
subscriptionActions .receiveRetainedMessages |
The
|
Optional with default of "onSubscription"
|
enum |
|
|
subscriptionActions .subscriptionAction |
The "subscriptionAction" property specifies whether the action will add or remove the specified subscription. |
Optional with default of "add"
|
string enum |
|
|
subscriptionActions .subscriptionIdentifier |
The |
Optional with default of 268435455
|
integer |
0 to 268435455
|
|
subscriptionActions .topicFilter |
The |
Optional with default of ""
|
UTF-8 string | topic names |
|
subscriptionActions .userProperties |
The "userProperties": [
{
"key": "some key",
"value": "some value"
}
] |
Optional with default of null
|
array | 0 or more key/value pairs |
Response properties ("result")
| Property | Description | Type | Limits (inclusive) |
|---|---|---|---|
subscriptions |
The "subscriptions" property details information about each subscription that matches the parameters of the request. |
array |
|
|
subscriptions .debugInfo |
The "debugInfo" property specifies debugging information for use in troubleshooting. |
object | No limit |
|
subscriptions .errorCode |
The "errorCode" property indicates an error when set to a non-zero integer or success when 0.
|
integer | -2147483648 to 2147483647 |
|
subscriptions .errorMessage |
The "errorMessage" property displays a human-readable error message. |
string | 0 to 256 bytes |
|
subscriptions .matchingTopics |
The "matchingTopics" property lists the topics that match the parameters specified in the request. |
array | No limit |
|
subscriptions .topicFilter |
The |
UTF-8 string | topic names |