---
source_url: https://www.pubnub.com/docs/sdks/arduino/api-reference/publish-and-subscribe
title: Publish/Subscribe API for Arduino SDK
updated_at: 2026-06-24T11:05:49.578Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Publish/Subscribe API for Arduino SDK

## Publish

The `publish()` function is used to send a message to all subscribers of a channel. To publish a message you must first specify a valid `publish_key` at initialization. A successfully published message is replicated across the PubNub Real-Time Network and sent simultaneously to all subscribed clients on a channel.

Messages in transit can be secured from potential eavesdroppers with SSL/TLS by setting ssl to true during initialization.

##### Publish anytime

It's not required to be subscribed to a channel in order to publish to that channel.

##### Message data

The message argument can contain any JSON serializable data, including: Objects, Arrays, Ints and Strings. `data` should not contain special Arduino classes or functions as these will not serialize. String content can include any single-byte or multi-byte UTF-8 character.

##### Message size

The maximum number of characters per message is 32 KiB by default. The maximum message size is based on the final escaped character count, including the channel name. An ideal message size is under 1800 bytes which allows a message to be compressed and sent using single IP datagram (1.5 KiB) providing optimal network performance.

If the message you publish exceeds the configured size, you will receive the following message:

##### Message too large error

```c
["PUBLISHED",[0,"Message Too Large","13524237335750949"]]
```

For further details, check [Calculating Message Payload Size Before Publish](https://support.pubnub.com/hc/en-us/articles/360051495932-Calculating-Message-Payload-Size-Before-Publish).

:::tip Need larger messages?
Our platform is optimized for payloads up to 32 KiB. PubNub supports larger messages, but increasing the limit requires a verification of compatibility with your use case.
Talk to [our team](https://www.pubnub.com/company/contact-sales/) to discuss increasing the message size limit for your use case.
:::

##### Message publish rate

Messages can be published as fast as bandwidth conditions will allow. There is a soft limit based on max throughput since messages will be discarded if the subscriber can't keep pace with the publisher.

For example, if 200 messages are published simultaneously before a subscriber has had a chance to receive any messages, the subscriber may not receive the first 100 messages because the message queue has a limit of only 100 messages stored in memory.

##### Publishing to multiple channels

It is not possible to publish a message to multiple channels simultaneously. The message must be published to one channel at a time.

##### Publishing messages reliably

There are some best practices to ensure messages are delivered when publishing to a channel:

* Publish to any given channel in a serial manner (not concurrently).
* Check that the return code is success (e.g. `[1,"Sent","136074940..."]`)
* Publish the next message only after receiving a success return code.
* If a failure code is returned (`[0,"blah","<timetoken>"]`), retry the publish.
* Avoid exceeding the in-memory queue's capacity of 100 messages. An overflow situation (aka missed messages) can occur if slow subscribers fail to keep up with the publish pace in a given period of time.
* Throttle publish bursts in accordance with your app's latency needs e.g. Publish no faster than 5 msgs per second to any one channel.

### Method(s)

To `Publish a message` you can use the following method(s) in the Arduino SDK:

:::note
If you want to include the publisher information in your message payload, you need to set the client UUID manually and explicitly include it in your message payload. Refer to [Sending publisher UUID](https://www.pubnub.com/docs/sdks/arduino/api-reference/configuration#uuid) for more information.
:::

```c
Pubnub_BASE_CLIENT *publish(const char *channel, const char *message, int timeout = 30);
```

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| channel | const | Yes |  | The `channel` to publish to. |
| message | const | Yes |  | The `JSON` `message` to publish. |
| timeout | int | Optional |  | `Timeout` of the publish, in seconds. Defaults to `30`. |

### Sample code

#### Publish a message to a channel:

```c
PubNub_BASE_CLIENT *client = PubNub.publish("my_channel", "\"message\"");
if (client != 0) {
    client->stop();
}
```

:::note
Before running the above publish example, either using the [Debug Console](https://www.pubnub.com/docs/console/) or in a separate script running in a separate terminal window, [subscribe to the same channel](#subscribe) that is being published to.
:::

### Rest response from server

| Type | Description |
| --- | --- |
| `Pubnub_BASE_CLIENT` | Pointer to the object to be used to check the outcome and get the response from PubNub. On failure to start publishing it's `0`. |

## Subscribe

This function causes the client to create an open TCP socket to the PubNub Real-Time Network and begin listening for messages on a specified `channel`. To subscribe to a `channel` the client must send the appropriate `subscribe_key` at initialization. By default a newly subscribed client will only receive messages published to the channel after the `subscribe()` call completes.

:::warning
**Unsubscribing** from all channels, and then **subscribing** to a new channel Y is not the same as subscribing to channel Y and then unsubscribing from the previously-subscribed channel(s). Unsubscribing from all channels resets the last-received `timetoken` and thus, there could be some gaps in the subscription that may lead to message loss.
:::

### Method(s)

To `Subscribe to a channel` you can use the following method(s) in the Arduino SDK:

```c
PubnubSubClient *subscribe(const char *channel, int timeout = 30);
```

| Parameter | Description |
| --- | --- |
| `channel` *Type: const char* | The `channel` to subscribe to. |
| `timeout`Type: int | `Timeout` of the subscribe, in seconds. Defaults to `30`. |

### Sample code

Subscribe to a channel:

```c
PubSubClient *sclient = Pubnub.subscribe("my_channel");
if (sclient != 0) {
    while (sclient->wait_for_data()) {
        Serial.write(sclient->read());
    }
    sclient->stop();
}
```

### Response

The following objects will be returned in the Subscribe Message Response

| Type | Description |
| --- | --- |
| `PubSubClient` | Pointer to the object to be used to check the outcome and get the messages int the response from Pubnub. On failure to start publishing it's `0`. |