---
source_url: https://www.pubnub.com/docs/sdks/eon-map/api-reference/publish-and-subscribe
title: Publish/Subscribe API for EON Map SDK
updated_at: 2026-06-30T11:07:10.882Z
---

> 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 EON Map 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 `publishKey` 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 EON Map classes or functions as these will not serialize. String content can include any single-byte or multi-byte UTF-8 character.

:::warning Don't Use JSON.stringify
It is important to note that you should not use `JSON.stringify()` when sending signals/messages via PubNub. Why? Because the serialization is done for you automatically. Instead just pass the full object as the message payload. PubNub takes care of everything for you.
:::

#### 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

```javascript
["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 EON Map SDK:

```javascript
publish( {Object message, String channel, Boolean storeInHistory, Boolean sendByPost, Object meta, Number ttl }, Function callback )
```

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| message | Object | Yes |  | The `message` may be any valid JSON type including objects, arrays, strings, and numbers. |
| channel | String | Yes |  | Specifies `channel` name to publish messages to. |
| storeInHistory | Boolean | Optional | `true` | If `true` the messages are stored in history. If `storeInHistory` is not specified, then the history configuration on the key is used. |
| sendByPost | Boolean | Optional | `false` | If `true` the messages are sent via POST. |
| meta | Object | Optional |  | Publish extra `meta` with the request. |
| ttl | Number | Optional |  | Set a per message time to live in Message Persistence. If storeInHistory = true, and ttl = 0, the message is stored with no expiry time., If storeInHistory = true and ttl = X (X is an Integer value), the message is stored with an expiry time of X hours unless you have message retention set to Unlimited on your keyset configuration in the Admin Portal., If storeInHistory = false, the ttl parameter is ignored., If ttl is not specified, then expiration of the message defaults back to the expiry value for the key. |
| callback | Function | Optional |  | Executes on a successful/unsuccessful `publish`. |

### Sample code

#### Publish a message to a channel

```javascript
pubnub.publish(
    {
        message: {
            such: 'object'
        },
        channel: 'my_channel',
        sendByPost: false, // true to send via post
        storeInHistory: false, //override default Message Persistence options
        meta: {
            "cool": "meta"
        }   // publish extra meta with the request
    },
    function (status, response) {
        if (status.error) {
            // handle error
            console.log(status)
        } else {
            console.log("message Published w/ timetoken", response.timetoken)
        }
    }
);
```

:::note Subscribe to the channel
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 that is being published to.
:::

### Response

```json
type PublishResponse = {
    timetoken: number
}
```

### Other examples

#### Publishing messages

The function below is called `connect` and fires when the `pubnub_mapbox` library is ready. This function uses the included PubNub library to pubnub.publish() packets to the pubnub.subscribe() call waiting inside the Mapbox framework. Notice how the `subscribeKey` and `channel`/`channels` matches.

```javascript
function connect() {

    let point = {
        latlng: [37.370375, -97.756138]
    };

    let pn = new PubNub({
        publishKey:   'YOUR_PUB_KEY', // replace with your own pub-key
        subscribeKey: 'YOUR_SUB_KEY'  // replace with your own sub-key
    });

    setInterval(function(){
        let new_point = JSON.parse(JSON.stringify(point));

        new_point.latlng = [
            new_point.latlng[0] + (getNonZeroRandomNumber() * 0.1),
            new_point.latlng[1] + (getNonZeroRandomNumber() * 0.2)
        ];

        pn.publish({
            channel: channel,
            message: [new_point] // even a single point should be an array
        });

    }, 500);

};

let map = eon.map({
    pubnub: pn,
    id: 'map',
    mbToken: 'pk.eyJ1IjoiaWFuamVubmluZ3MiLCJhIjoiZExwb0p5WSJ9.XLi48h-NOyJOCJuu1-h-Jg',
    mbId: 'ianjennings.l896mh2e',
    channels: [channel],
    connect: connect
});
```