Connecting and disconnecting

Every client that wants to send or receive messages must connect to the messaging broker (using the CONNECT MQTT control packet), and can disconnect at the end of the session using the DISCONNECT MQTT control packet. To keep the session connected, a client must continuously send a control packet to the broker. The maximum interval between packets being sent must be one and a half times the Keep-Alive value.

Where can I find an MQTT client / library?

Applications using the messaging service typically use an available client library providing MQTT functionality. There are MQTT libraries for many languages and environments such as:

  • Xively provides a C Client Library with MQTT and TLS support for device firmware. In microcontroller environments, use of the Xively C Client Library is recommended.
  • Mosquitto has downloads for Windows, Mac, and Linux. These include software libraries and command lines.
  • Pahoprovides clients for several languages, including Java, Python, and C/C++.
  • mqtt.js provides a javascript library which can be used from browsers (via WebSockets) and backend environments (native).

See Xively Libraries for the full list of recommended libraries in the major languages, and more detail on the Xively embedded client in C.

What happens when the device is connected?

Connecting to the broker refers to creating the TCP connection between the MQTT client (a device, user, or an application) and the Xively broker. Normally, an MQTT client creates a connection, and the connection remains open indefinitely for the client to publish and receive messages.

There are three ways to connect to the Xively broker from an application. The method chosen depends on the application environment and needs.

Important: The size limit for the message broker is 128kb (kilobytes).

Three ways to connect the Xively MQTT broker

Native MQTT

This is the recommended method and can be used when a client can make native connections. The Xively broker runs on ports 443 and 8883 (either port can be used). Port 443 may be suitable for devices on corporate networks with firewalls.

Websockets

Websockets can be used when a client does not support native MQTT connections, such as a web browser. Port 443 is used.

Platform API (HTTPS)

Platform APIs are useful when a client does not have MQTT, or does not want to use MQTT. This method supports only the publishing of messages, and not subscribing. The Platform API method is bettersuited to situations where a client publishes infrequently. The broker supports persistent HTTPS connections and HTTPS pipelining.


What's Next

Look at the connection modes available with Xively

Connection Modes

Connecting and disconnecting