How Many Characters Fit in a 32KB PubNub Message?

4 min read Markus Kohler on Feb 21, 2024

The maximum length of a PubNub message is 32KB (see: PubNub SDK docs and PubNub soft and hard limits). If it’s over 32KB, your message will not be delivered. But what does 32KB really look like for a message? If you are building an IoT-based solution, 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 easily fit in 32KB. But there are many use cases that require more information in a single PubNub message. Think chat messages or analysis from a cognitive service. 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:

To calculate message size, 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 and for ideas on how you might structure it for one of our 70+ other supported SDKs.


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, by Thomas Jefferson, could be encoded as a PubNub message in the following manner:

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. With PubNub's message persistence feature, we could encode a significant portion of this short story:

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.

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?” Also, remember that encrypting messages can add 30% and that messages written in unicode take up more room — plan accordingly. Pro Tip: Keeping your messages < 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 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 and if you get stuck, reach out — we want you to be successful. If you build something and open source it, drop a line to our Developer Relations team — we would love to see it.

This Blog as a PubNub Message

This blog post is 10.5k

Getting Started

Get started with PubNub today! Sign up for a PubNub account check out your keyset to obtain your publish key and subscribe key, and check out everything PubNub has to offer including:

  1. Publish: Send messages whenever user input is updated, such as text updates, emoji reactions, sent files, and other complex metadata.

  2. Subscribe: Receive new messages to refresh users' screens.

  3. Presence: Update and detect the online status of users.

  4. Message Persistence: Display any received messages once users login to the app or track project and document revisions.

  5. Mobile Push Notifications: Notify mobile users who are away from the app about any chat messages, project updates, or application updates.

  6. App Context: Store information about your user in one place without setting up or calling your database.

  7. Access Manager: Restrict access for private conversations, channel rooms, documents, and projects for specific users.

  8. Functions: Translate messages, censor inappropriate messages, announce the arrival of new users, and notify other users of mentions.

  9. Events & Actions: Centrally manage events in your application’s ecosystem and trigger business logic without code.