Publish unicode string using solclientjs

Hello hello! I am updating this thread…! ? I have recently stumbled onto this particular issue again where a JavaScript publisher was sending a String as a raw binary attachment, and it contained the GBP symbol £. And it wasn’t getting encoded properly into UTF-8, it was sending it as byte that doesn’t exist in UTF-8. I’ve done some research and thought I’d post an update.

I started off using my approach above for converting to a UTF-8 string:


function encode_utf8(s) {
    return unescape(encodeURIComponent(s));
}

And it still works great, as expected. But did some research and turns out escape() / unescape() have been deprecated for a LONG time. So while this approach still works, it is not current best practices.

I found some other posts that talk about TextEncoder object, and it seems to work well. TextEncoder can either generate a new Uint8Array on each encode() invocation (for low performance apps), or you can predefine the array and reuse it with encodeInto(array) to save on memory thrashing.

However! I noticed that I could not get my subscriber to properly detect when I was sending a raw array in the binary attachment… it was always returning a type of String. So I checked the Docs , and noticed that for “older” versions of our JavaScript API, it always returns a Latin1 string. To fix this, all I had to do was update the factory profile to the newer 10.5 : ??


var factoryProps = new solace.SolclientFactoryProperties();
factoryProps.profile = solace.SolclientFactoryProfiles.version10_5;
solace.SolclientFactory.init(factoryProps

Then my subscriber’s call to getBinaryAttachment() was returning an array as expected. Unexpectedly, I didn’t even have to use the TextDecoder on the other side, JavaScript just knew that it was a UTF-8 String!?

Hopefully this helps anyone in the future stumbling onto this. The publisher should do something like:


const weirdText = "Hello World! £¥→ÐĞ???";
const encoder = new TextEncoder();  // probably best to make this global and reuse
const u8array = encoder.encode(weirdText);
message.setBinaryAttachment(u8array);

Then on the subscriber side, make sure you’re using SolclientFactoryProfiles.version10_5 and the String will pop out properly formatted as expected…! ??

If the factory profile version is left at 10, it looks like this:


[19:09:36] solace/js/test/topic: Hello World! £¥→ÐĞ🎅🎉