Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Calculating a PubNub Message Payload Size. This is necessary to prevent yourself from getting a "Message Too Large" gateway response.
(function(){
// Calculating a PubNub Message Payload Size.
function calculate_payload_size( channel, message ) {
return encodeURIComponent( channel + JSON.stringify(message) ).length + 97;
}
})();
@stephenlb

This comment has been minimized.

Copy link
Owner Author

stephenlb commented Jan 4, 2014

Calculating a PubNub Message Payload Size

You'll want to calculate your message size before sending to PubNub in order to prevent "Message Too Large" Gateway Responses. Normally this isn't necessary if you generally cap your message size below 500 bytes, but if you want to approach the absolute limit, here is the solution:

Calculation Usage Example

var channel = "my_channel_name";
var message = { "name" : "Rob Middleton", "company" : "i.TV", "language" : "JavaScript" };
var size    = calculate_payload_size( channel, message );

console.log( "Payload Size: ", size );

OUTPUT: Payload Size: 194

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.