How Many Characters Fit in a 32KB PubNub Message?
Published: 2024-02-21 13:42:24

The maximum length of a PubNub message is 32KB (see: [PubNub SDK docs](https://www.pubnub.com/docs) and [PubNub soft and hard limits](https://www.pubnub.com/docs/sdks/javascript)). If it’s over 32KB, your message will [not be delivered](https://support.pubnub.com/hc/en-us/articles/360051495852-What-happens-to-messages-that-exceed-the-size-limit). But what does 32KB really look like for a message? If you are building an [IoT-based solution](https://support.pubnub.com/hc/en-us/articles/360051495872-What-is-the-maximum-message-size), you are probably not too worried about message size. You are likely just sending short update information, like longitude and latitude for geolocation needs, temperature or sensor data for embedded systems, or short instructions to control lights or other smart appliances. Likewise, leaderboards for real-time, large-scale [online games](https://www.pubnub.com/solutions/gaming/) easily fit in 32KB. But there are many use cases that require more information in a single PubNub message. Think [chat messages](https://www.pubnub.com/solutions/chat/) or analysis from a [cognitive service](https://www.pubnub.com/blog/the-many-uses-of-cognitive-services/). In this post, we'll use a chat message as our example.

Let’s Do Some Math(s)
---------------------

To figure out the total size of a payload, first, we have to define what a chat message might look like. A chat message for our calculating use includes the entire payload: the channel, any ‘side channel’ information, and the actual text itself. When packaged up by PubNub, it will also include envelope information including the timestamp (timetoken) and other control information needed for routing. The message you publish from a client might look something like:

```js
var channel = "doc_patient_comms_a";
var message = {
    "name" : "Dr. Blackwell",
    "company" : "Sunnydale Memorial",
    "uuid" : "uuid-string",
    "text" : "Your test results are back and everything looks normal."
};
```

To [calculate message size](https://support.pubnub.com/hc/en-us), combine the channel name and message payload (JSON) string, URI encode the result (encodeURIComponent in Javascript) and then check the message length (String.length in Javascript). A best practice is to then add 100 to the result to account for the PubNub envelope (metadata) information. See our support document for a [full Javascript function](https://support.pubnub.com/hc/en-us) and for ideas on how you might structure it for one of our 70+ other supported SDKs.

Examples
--------

Now that we have a way to calculate message size, let's explore the capabilities of the PubNub API by examining historical texts and their potential transmission as PubNub messages. For instance, [The Declaration of Independence](https://www.archives.gov/founding-docs/declaration-transcript), by Thomas Jefferson, could be encoded as a PubNub message in the following manner:

```js
var channel = "continental_congress_declaration_subcommittee";
var message = {
    "name" : "Mr. Jefferson",
    "uuid" : "4f4ec1b4-4957-4125-9103-c6edcfe37770",
    "text" : "The unanimous Declaration of the thirteen united States of America, When in the Course of human events[…]our sacred Honor."
};
```

Using JSON objects for message content, this message calculates to 11.4KB. Thus, Mr. Jefferson could send two copies of the Declaration in a single PubNub message, showcasing the efficiency of the PubNub API - global data transmission with minimal latency in less than 250ms.

Consider another example, "Routine for a Hornet" by Don Berry, a work from [Project Gutenberg](https://www.gutenberg.org/). With PubNub's message persistence feature, we could encode a significant portion of this short story:

```js
var channel = "worlds_of_science_fiction_editors";
var message = {
    "name" : "Don Berry",
    "uuid" : "99c8361f-ac61-47f5-9b5f-22095227e6e3",
    "text" : "ROUTINE for a HORNET[…]in any case."
};
```

In one payload, you can send about 85% of this short story from “The Worlds of Science Fiction”, but you’ll have to stop at the cliffhanger “…Nor could he have taken his hands away from the controls in any case,” and send the rest in a second message. However, since each message arrives in 50-250ms, it’s a short wait. British Goblins: Welsh Folk-lore, Fairy Mythology, Legends and Traditions by Sikes. This is a long tome (773kB file size). If someone wanted to send this, you would need to break it up into multiple pieces. In my testing, each chapter of each book fills up our payload. For instance, "BOOK 1 The Realm of Faerie, Chapter 1 - Fairy Tales and the Ancient Mythology…” is 23KB by itself. There is much to know about Fairyland.

```js
var channel = "british_goblin_researchers";
var message = {
    "name" : "Sikes",
    "uuid" : "8044e839-a3b2-4bab-b2d1-c52897299cf5",
    "text" : "BOOK I.[…]'Welsh Melodies.'"
};
```

Best Practices
--------------

As we’ve seen, 32KB is a reasonable amount of text. However, it is a best practice to test every user-generated message for size. If it is too big, we have some suggestions on how to adapt in our support article “[Can I send large messages with PubNub?](https://support.pubnub.com/hc/en-us/articles/360051495792-Can-I-send-large-messages-with-PubNub-#latest)” Also, remember that encrypting messages can add 30% and that messages written in double-byte languages take up more room - plan accordingly. Pro Tip: Keeping your messages &lt; 1.5KB in size will allow them to fit into a single TCP packet. What about images? Images can be sent when converted to base-64. That conversion adds significant overhead so, again, test your system. Check out our [DIY Snapchat clone](https://www.pubnub.com/blog/diy-snapchat-clone-capture-send-images-javascript-tutorial/) demo project for some ideas. Finally, what is 250ms? 250 milliseconds is shorter than it took you to read this sentence. With 15 points of presence around the world and a patented real-time architecture, PubNub delivers your payloads quickly.

A Final Thought
---------------

It can be tempting to attempt to answer every technical question before you begin a new project. The beauty of the PubNub SDKs is there is rarely only one right way. We have plenty of examples on our [blog](https://www.pubnub.com/blog/) and if you get stuck, [reach out](https://support.pubnub.com/hc/en-us) - we want you to be successful. If you build something and open source it, drop a line to our [Developer Relations team](mailto:devrel@pubnub.com) - we would love to see it.

### This Blog as a PubNub Message

```js
var channel = "my_channel";
var message = {
    "name" : "Mark Williams",
    "uuid" : "d75dace8-1f15-4b8e-80a3-646ad97b58de",
    "text" : "The maximum length […] we would love to see it."
};
```

This blog post is 10.5k

### Getting Started

Get started with PubNub today! [Sign up for a PubNub account](https://www.pubnub.com/docs/general/basics/set-up-your-account) check out your keyset to obtain your publish key and subscribe key, and check out everything PubNub has to offer including:

1.  [Publish](https://www.pubnub.com/docs/messages/publish): Send messages whenever user input is updated, such as text updates, emoji reactions, sent files, and other complex [metadata](https://www.pubnub.com/docs/metadata/channel-metadata).
    
2.  [Subscribe](https://www.pubnub.com/docs/messages/receive): Receive new messages to refresh users' screens.
    
3.  [Presence](https://www.pubnub.com/docs/presence/overview): Update and detect the online status of users.
    
4.  [Message Persistence](https://www.pubnub.com/docs/storage): Display any received messages once users login to the app or track project and document revisions.
    
5.  [Mobile Push Notifications](https://www.pubnub.com/docs/push/send): Notify mobile users who are away from the app about any chat messages, project updates, or application updates.
    
6.  [App Context](https://www.pubnub.com/docs/general/metadata/channel-metadata): Store information about your user in one place without setting up or calling your database.
    
7.  [Access Manager](https://www.pubnub.com/docs/security/access-control): Restrict access for private conversations, channel rooms, documents, and projects for specific users.
    
8.  [Functions](https://www.pubnub.com/docs/general/serverless/functions/overview): Translate messages, censor inappropriate messages, announce the arrival of new users, and notify other users of mentions.
    
9.  [Events & Actions](https://www.pubnub.com/docs/general/serverless/events-and-actions/overview): Centrally manage events in your application’s ecosystem and trigger business logic without code.