All Products
Search
Document Center

Simple Message Queue (formerly MNS):Select an encoding method for the message body

Last Updated:Oct 22, 2024

The Simple Message Queue (SMQ) client comes with the option to Base64-encode messages. This topic describes when to use Base64 encoding in different production and consumption scenarios.

Background information

Base64 encoding is used to convert binary data into ASCII strings. It is widely used to transmit data in environments that require text processing.

Advantages of using Base64 encoding

  • Compatibility: Base64-encoded strings consist of printable ASCII characters and are suitable for transmission in text formats such as email, JSON, and XML. This prevents garbled text that may occur with binary data.

  • Security: Although Base64 encoding is not an encryption method, it obscures the raw binary data, preventing direct viewing in some pure text formats. This improves data security to some extent.

  • Simplicity: Base64 encoding and decoding are relatively simple processes. Many programming languages and libraries support Base64 encoding.

Disadvantages of using Base64 encoding

  • Larger data size: Base64 encoding increases the data size by approximately 33%. If you Base64-encode a large binary file, the size of the encoded file is larger than that of the original file.

  • Lower efficiency: Decoding requires additional computing resources, especially when a large amount of data needs to be processed. This may significantly increase performance overhead.

  • Low readability: The encoded message body is not human-readable.

Suggestions

Queue-based messaging model

If the message body does not contain special characters, we recommend that you do not use Base64 encoding.

  • To send a message, use the message.setMessageBodyAsRawString method to set the message body.

  • To receive a message, use the message.getMessageBodyAsRawString method to obtain the message body.

Topic-based messaging model (subscription type: Queue or HTTP)

In the topic-based messaging model, the NotifyContentFormat parameter specifies the message format delivered to subscribers. Three message formats are supported: SIMPLIFIED, JSON, and XML. For more information, see the "Message formats" section of the Subscription topic.

  • SIMPLIFIED: If the message body does not contain special characters, we recommend that you do not use Base64 encoding.

    • To send a message to a topic, use the RawTopicMessage method to initialize the message object.

    • To consume a message from a queue, use the message.getMessageBodyAsRawString() method to obtain the message body.

  • JSON or XML: If the strings are transmitted in a text format such as JSON or XML, we recommend that you use Base64 encoding.

    • To send a message to a topic, use the TopicMessage method to initialize the message object. In this case, the message body is Base64-encoded and stored in the Message field for transmission.

    • To consume a message from a queue, use the message.getMessageBodyAsRawString(); method to obtain the value of the Message field, and then perform Base64 decoding.

      JSONObject object = new JSONObject(message.getMessageBodyAsRawString());
      String jsonMessageData = String.valueOf(object.get("Message"));
      String messageBody = new String(Base64.decodeBase64(jsonMessageData));

Topic-based messaging model (subscription type: Direct Mail)

If the message body does not contain special characters, we recommend that you do not use Base64 encoding.

  • To send a message, use the message.setMessageBodyAsRawString method to set the message body.

  • To receive a message, use the message.getMessageBodyAsRawString method to obtain the message body.

Topic-based messaging model (subscription type: SMS)

If the message body does not contain special characters, we recommend that you do not use Base64 encoding.

  • To send a message, use the message.setMessageBodyAsRawString method to set the message body.

  • To receive a message, use the message.getMessageBodyAsRawString method to obtain the message body.

Topic-based messaging model (subscription type: Mobile Push)

If the message body does not contain special characters, we recommend that you do not use Base64 encoding.

  • To send a message, use the message.setMessageBodyAsRawString method to set the message body.

  • To receive a message, use the message.getMessageBodyAsRawString method to obtain the message body.