Important: This documentation is about an older version. It's relevant only to the release noted, many of the features and functions have been updated or replaced. Please view the current version.

Documentationbreadcrumb arrow Grafana k6breadcrumb arrow JavaScript APIbreadcrumb arrow k6/x/mqttbreadcrumb arrow Clientbreadcrumb arrow Client.connect( url, [options] )
Open source

Client.connect()

Establishes a connection to an MQTT broker. When the connection is successfully established, the connect event is triggered.

Signature

JavaScript
client.connect(url, options)

Parameters

ParameterTypeDescription
urlstringBroker URL (for example, mqtt://broker.emqx.io:1883)
optionsobjectOptional connection configuration
options.keepalivenumberKeep-alive interval in seconds (default: 60)
options.connect_timeoutnumberConnection timeout in milliseconds (default: 30000)
options.clean_sessionbooleanWhether to start with a clean session (default: true)
options.serversstring[]Array of broker URLs for failover
options.tagsobjectCustom tags for metrics (key-value pairs)

Supported broker URL schemas

SchemaDescription
mqtt://Plain TCP connection (no encryption)
mqtts://Secure connection over SSL/TLS
tcp://Alias for mqtt://
ssl://Alias for mqtts://
tls://Alias for mqtts://
ws://MQTT over WebSocket
wss://MQTT over secure WebSocket

If the schema is omitted, mqtt:// is used as the default.

SSL/TLS configuration

The extension uses standard k6 TLS configuration for all SSL/TLS settings. Configure certificates, verification, and other TLS-related options using k6’s environment variables and configuration files.