MQTT Vuser Functions > mqtt_connect

mqtt_connect

Establishes connection.

C Language

int mqtt_connect( MQTT handle, const char* url);

Example MQTT Vuser Functions

Arguments

Name Comments
handle The MQTT client handle returned by mqtt_create.
url

Address of the broker server in the format: tcp://host:port or ssl://host:port, where host is domain name, IPv4 address or the IPv6 address in square brackets. Example of IPv6 address: [2001:0db8:85a3:0000:0000:8a2e:0370:7334].

If the port is not specified, the following default ports are used:

  • tcp://host:1883
  • ssl://host:8883

General

mqtt_connect connects the client to the broker.

If invalid arguments are passed, the Vuser aborts, even if Continue on error is set.

Return Values

MQTT_SUCCESS or MQTT_FAIL

Parameterization

Standard parameterization is available for this function.

Example

This example shows how to create a connection handle, connect, and disconnect.

/* Declare MQTT client handle variable */

MQTT client;


/* Create client object and assign its handle to a variable */

client = mqtt_create();


/* Connect MQTT client to a broker at test.broker.com using TCP port number 1883 and insecure channel */

mqtt_connect(client, "tcp://test.broker.com:1883");


/*

..... Script logic .....

*/


/* Disconnect MQTT client from the broker */

mqtt_disconnect(client);