Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Last active January 2, 2016 04:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenlb/8249445 to your computer and use it in GitHub Desktop.
Save stephenlb/8249445 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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