does MQTT library support 2 brokers?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

does MQTT library support 2 brokers?

Jump to solution
1,769 Views
ping1
Contributor V

Dear all

I am using MQTT library under lwip example, I wonder whether it supports two brokers at same time. If so, how to do it?

Regards!

Ping

Labels (1)
Tags (2)
0 Kudos
1 Solution
1,747 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi @ping1 ,

  Sorry for my misleading previously!

    SDK LWIP can support more than one MQTT broker at one time, you just need to define more MQTT broker, attached code is for your reference, and it has been tested, you can refer to it.

uint8_t *serverName[MAXMQTT_NUM] = {EXAMPLE_MQTT_SERVER_HOST, MQTT_SERVER_HOST2}; 
uint32_t serverPort[MAXMQTT_NUM] = {EXAMPLE_MQTT_SERVER_PORT, MQTT_SERVER_PORT2}; 

for(uint32_t i=0; i<MAXMQTT_NUM; i++)
{
mqtt_client[i] = mqtt_client_new(); 
}

 

for(uint32_t i=0; i<MAXMQTT_NUM; i++)
{
if (ipaddr_aton(serverName[i], &mqtt_addr[i]) && IP_IS_V4(&mqtt_addr))
{
/* Already an IP address */
err = ERR_OK;
}
else
{
/* Resolve MQTT broker's host name to an IP address */
PRINTF("Resolving \"%s\"...\r\n", serverName[i]);
err = netconn_gethostbyname(serverName[i], &mqtt_addr[i]);
}
}

Wish it helps you!

If you still have questions about it, please kindly let me know.

Best Regards,

Kerry

View solution in original post

0 Kudos
2 Replies
1,733 Views
mjbcswitzerland
Specialist V

Hi

MQTT operates over a TCP socket or a secure TCP socket and so any number of instances should be possible. This has nothing to do with Ethernet or the number of Ethernet controllers available but is restricted only by the resources available (mainly RAM).

Each MQTT client can have its own MQTT broker settings and behave independently of other MQTT clients that are operating on the top of the same TCP/IP stack.

I don't know details of the LWIP MQTT implementation but with the uTasker MQTT client any number can be used, whereby it is to note that the TLS layer, when using secure MQTT connections is where the most RAM is required and so is more of the practical limit if multiple secure sessions are used in parallel.

Regards

Mark

 

0 Kudos
1,748 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi @ping1 ,

  Sorry for my misleading previously!

    SDK LWIP can support more than one MQTT broker at one time, you just need to define more MQTT broker, attached code is for your reference, and it has been tested, you can refer to it.

uint8_t *serverName[MAXMQTT_NUM] = {EXAMPLE_MQTT_SERVER_HOST, MQTT_SERVER_HOST2}; 
uint32_t serverPort[MAXMQTT_NUM] = {EXAMPLE_MQTT_SERVER_PORT, MQTT_SERVER_PORT2}; 

for(uint32_t i=0; i<MAXMQTT_NUM; i++)
{
mqtt_client[i] = mqtt_client_new(); 
}

 

for(uint32_t i=0; i<MAXMQTT_NUM; i++)
{
if (ipaddr_aton(serverName[i], &mqtt_addr[i]) && IP_IS_V4(&mqtt_addr))
{
/* Already an IP address */
err = ERR_OK;
}
else
{
/* Resolve MQTT broker's host name to an IP address */
PRINTF("Resolving \"%s\"...\r\n", serverName[i]);
err = netconn_gethostbyname(serverName[i], &mqtt_addr[i]);
}
}

Wish it helps you!

If you still have questions about it, please kindly let me know.

Best Regards,

Kerry

0 Kudos