Vivox text messages are limited to 320 bytes (UTF-8). To send longer messages, split them into multiple smaller JSON-encoded messages, each containing a portion of the text and metadata for reassembly. Send these using standard Vivox message methods. Example code demonstrates chunking a string by UTF-8 byte length to fit within limits, ensuring proper message reconstruction on the receiving end.
Procedure:
Vivox text messages are limited to 320 byte in length (based on UTF-8 encoding, characters can take up between 1 and 4 bytes). As a workaround, the end-user's long message can be broken into multiple smaller messages wrapped in JSON encoding that will be reassembled when received and displayed as a singular message.
Example:
Note: for an easier to read example, we will pretend that the limit is 50 bytes (not 320 bytes) and we will include whitespace in our JSON encoding for readability
.The original message is:
Vivox offers additional features for speech-to-text transcriptions and text-to-speech.
This is split into multiple messages using JSON encoding. For this example, we are keeping the keys as short as possible, with b for body, i for the current message index, and t for the total number of messages. The current message index does not technically need to be included, as the receiving side can maintain a count of received messages. It does, however, allow for easier debugging.
Here we have some example JSON. We're allowing for extra space for the i and t values to become triple digit values (1 to 999). In this simple example, all the values are basic ASCII, which take up singular bytes in UTF8 encoding.
{ "b": "Vivox offers additi", "i": 1, "t": 5 }
{ "b": "onal features for s", "i": 2, "t": 5 }
{ "b": "peech-to-text trans", "i": 3, "t": 5 }
{ "b": "criptions and text-", "i": 4, "t": 5 }
{ "b": "to-speech.", "i": 5, "t": 5 }Each of these messages will be sent using the standard SendChannelTextMessageAsync or SendDirectTextMessageAsync method. The receiving clients will determine how many messages need to be received before reassembly.
Here is some example code that can be used to chunk out the original message for your JSON encoding. maxBytes should be the number of bytes available based on your JSON encoding (e.g., if your JSON encoding, without any string can use a maximum of 120 bytes, your maxBytes should be 200 [320 - 120]).
using System;
using System.Collections.Generic;
using System.Text;
public static class Utf8Chunker
{
public static List<string> ChunkStringByUtf8Bytes(string input, int maxBytes)
{
var utf8 = Encoding.UTF8;
byte[] bytes = utf8.GetBytes(input);
List<string> chunks = new List<string>();
int start = 0;
while (start < bytes.Length)
{
// Try to take up to maxBytes, but not past the end of the array
int end = Math.Min(start + maxBytes, bytes.Length);
// Move end back to the last valid UTF8 boundary (if not at the end)
while (end > start && end < bytes.Length && (bytes[end] & 0xC0) == 0x80)
{
end--;
}
// Safety: if we couldn't find a boundary, just take the rest (should not happen with valid UTF8 input)
if (end == start)
end = bytes.Length;
string chunk = utf8.GetString(bytes, start, end - start);
chunks.Add(chunk);
start = end;
}
return chunks;
}
}