--- layout: default title: API Docs ---

These docs refer to the latest version of the library on GitHub

Constructors
Functions
Other

PubSubClient (server, port, callback, client)

Creates a client instance, with the server specified by IP address.

Parameters

PubSubClient (serverDNS, port, callback, client)

Creates a client instance, with the server specified by DNS name.

Parameters

boolean connect (clientID)

Connects the client.

Parameters
Returns

boolean connect (clientID, willTopic, willQoS, willRetain, willMessage)

Connects the client with a Will message specified.

Parameters
Returns

boolean connect (clientID, username, password)

Connects the client with a username and password specified.

Parameters
Returns

boolean connect (clientID, username, password, willTopic, willQoS, willRetain, willMessage)

Connects the client with a Will message, username and password specified.

Parameters
Returns

void disconnect ()

Disconnects the client.

int publish (topic, payload)

Publishes a string message to the specified topic.

Parameters
Returns

int publish (topic, payload, length)

Publishes a message to the specified topic.

Parameters
Returns

int publish (topic, payload, length, retained)

Publishes a message to the specified topic, with the retained flag as specified.

Parameters
Returns

int publish_P (topic, payload, length, retained)

Publishes a message stored in PROGMEN to the specified topic, with the retained flag as specified.

Parameters
Returns

boolean subscribe (topic)

Subscribes to messages published to the specified topic.

Parameters
Returns

boolean loop ()

This should be called regularly to allow the client to process incoming messages and maintain its connection to the server.

Returns

int connected ()

Checks whether the client is connected to the server.

Returns

Configuration Options

The following configuration options can be used to configure the library. They are contained in PubSubClient.h.

MQTT_MAX_PACKET_SIZE
Sets the largest packet size, in bytes, the client will handle. Any packet received that exceeds this size will be ignored.

Default: 128 bytes

MQTT_KEEPALIVE
Sets the keepalive interval, in seconds, the client will use. This is used to maintain the connection when no other packets are being
sent or received.

Default: 15 seconds

Subscription Callback

If the client is used to subscribe to topics, a callback function must be provided in the constructor. This function is called when new messages arrive at the client.

The callback function has the following signature:

 void callback(char* topic, byte* payload, unsigned int length)
Parameters

Internally, the client uses the same buffer for both inbound and outbound messages. After the callback function returns, or if a call to either publish or subscribe is made from within the callback function, the topic and payload values passed to the function will be overwritten. The application should create its own copy of the values if they are required beyond this.