IN THIS ARTICLE
Subscribe to Our Newsletter
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 realtime, 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 and other control information needed for routing.
The message you publish from a client might look something like:
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, combine the channel string and message 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 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.
Examples
Now that we have a way to calculate message size, let’s take some historical texts and plug them in to see how big of a document we could send.
The Declaration of Independence, by Thomas Jefferson published as a PubNub message:
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." };
This message calculates to 11.4KB. So, Mr. Jefferson could send two copies of the Declaration in a single PubNub message. It would then publish anywhere in the world in under 250ms. Much faster than by messengers on horses and boats.
Routine for a Hornet by Don Berry, from Project Gutenberg (tested using the “Plain Text UTF-8” version with Project Gutenberg license text removed):
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.
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?”
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 < 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 realtime 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.
Coda
var channel = "pubnub_blog"; 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