what method should I use to return the contents of a solace.message in human readable string
Hi,
I am exploring the node.js SOLACE REST API. I got the pub/sub to work with the sample code to work substituting vpn/url/credentials.
The received message, however, has the payload plus a lot of other things and I couldn't isolate the payload by itself. I looked up the API (https://docs.solace.com/API-Developer-Online-Ref-Documentation/js/index.html) for class solace.Message and I saw methods associated with message such as getBinaryAttachment() and dump(Number flags). None of them does what I want which is to return a human readable string of the payload and nothing else. Can somebody help?
Thanks,
Eddie
Best Answers
-
Hi Eddie, it looks like you have an Structured Data Type (SDT) in the message. SDTs are stored as binary data in a machine architecture agnostic format. The SDT contains some header like fields such as JMSDeliveryMode. Some of these fields are added by the API, some by the broker on message ingress. That may help with understanding but it doesn't answer your question.
So, how to extract the payload that you added? Well, knowing the message type (i.e. is this text, binary etc? See solace.MessageType ) will help, so you could call mesage.getType() to find out. How is the payload added to the message in your publisher?5 -
Hi TomF,
Thanks for your response. The payload in this case was generated by running a java program which generates ActiveMQ messages (kind of like JMS messages). I displayed the message type and it shows up as "0 - I think it means BINARY (the other types are MAP(1), STREAM(2) and TEXT(3). I 've tried something like message.getBinaryAttachment.toString(,xy) and message.getSdtContainer but they didn't get me what I want.
Any ideas?
Thanks,Eddie
0
Answers
-
Hey Eddie!
The getBinaryAttachment() method on the message object returns the payload and the dump() returns everything in the messageYou can perhaps as well take a look at the sample Node.js code under the "Receiving a message" section https://solace.com/samples/solace-samples-nodejs/publish-subscribe/
Another helpful resource could be checking out the nodejs sample code on github. Here is an example of the subscriber outputting the content of the message using the getBinaryAttachment() method https://github.com/SolaceSamples/solace-samples-nodejs/blob/master/src/basic-samples/TopicSubscriber.js#L116-L118
Hopefully this helps
0 -
Tamimi,
Thanks for the response. I am familiar with the links you posted. The sample code that you referred to in the links was the exact source of my code. All I did was cloned the code, changed the only changed the pub/sub parameters to match my environment and I did get the pub/sub to work exact the output was not in the form that I can use. The following is an excerpt from my console log. The code that I used to display this was straight from the sample (line 112):
console.log('Received message: "' + message.getBinaryAttachment() + '", details:\n' + message.dump());
partial output follows. Any ideas?
Received message: "¬í sr ,com.solacesystems.jms.message.SolTextMessageõ�?ï�?$â L mTextt Ljava/lang/String;xr (com.solacesystems.jms.message.SolMessageû÷/Ñ_-iø I mJMSDeliveryModeZ mJMSDestinati
mJMSTimestampZ mJMSTimestampSetZ mReadOnlyPropertiesL mDMQEligiblet Ljava/lang/Boolean;L mDiscardIndicationq ~ L mElidingEligibleq ~ L mHTTPContentEncodingq ~ L mHTTPContentTypeq ~ L mIsDT
mJMSMessageIDq ~ L mJMSReplyToq ~ LmJMSTypeq ~ L mPropertiest Ljava/util/Map;L mmSenderIDq ~ L mTopicSequenceNumbert Ljava/lang/Long;xp ppppppppppppppsr java.uti
l.HashMapÚ�?Ã`Ñ F
loadFactorI thresholdxp?@ t EventTypet SAVEt EventNamet CONTACT_RECORD_SAVE_EVENTxppxtD{"dataObject":{"callDuration":416545,"preferredLanguageCode":"English","contactChanne
lType":"Phone","consumerLastName":"mDadbVSekoAX","linkRefId":-200309084220205,"consumerPhone":"4574339910","contactType":"Third Party","createdOn":"2020-03-09T17:57:11.716-05:00","inboundCallQueue":"El
igibility","contactRecordId":-200309084228850,"contactRecordEndTime":"2020-03-09T18:20:48.663-05:00","uiid":"DlVNubhvWnyQKelu","correlationId":"uLIhMAwsf","contactRecordStartTime":"2020-03-09T17:57:11.
716-05:00","contactCompaignType":"Enrollment Reminder","updatedBy":null,"organizationName":"Brg","contactRecordStatusType":"Dropped","updatedOn":null,"linkRefType":"consumer","consumerFirstName":"wKdlh
o","contactOutcome":"Reached Successfully","wrapUpTime":"00:00:59","createdBy":"txxhc","consumerAuthenticatedInd":"false","contactRecordType":"Inbound","contactReasonEditType":"Correcting Contact Reaso
n/Action","consumerEmail":"JZwQLlv@yoY.com"},"recordType":"Contact Record","action":"Create","eventCreatedOn":"2020-03-09T18:20:48.663-05:00","projectName":"StxdWRhnnXrrOsG","projectId":-20030908421825
6}x", details:
Destination: [Topic A/B]
AppMessageID: ID:fe80:0:0:0:890:ef43:a717:e2a2%eth12f8f2170bf5144bb0:41827
SendTimestamp: 1583796048663 (Mon Mar 09 2020 19:20:48 GMT-0400 (Eastern Daylight Time))
Class Of Service: COS1
DeliveryMode: DIRECT
User Property Map: 3 entries
Key 'EventName' (STRING): CONTACT_RECORD_SAVE_EVENT
Key 'EventType' (STRING): SAVE
Key 'JMSXUserID' (STRING): solace-cloud-clientBinary Attachment: len=2016
ac ed 00 05 73 72 00 2c 63 6f 6d 2e 73 6f 6c 61 ....sr.,com.sola
63 65 73 79 73 74 65 6d 73 2e 6a 6d 73 2e 6d 65 cesystems.jms.me
73 73 61 67 65 2e 53 6f 6c 54 65 78 74 4d 65 73 ssage.SolTextMes
73 61 67 65 f5 8f ef 0e cd 24 1e e2 03 00 01 4c sage.....$.....L....0 -
Hi Eddie, it looks like you have an Structured Data Type (SDT) in the message. SDTs are stored as binary data in a machine architecture agnostic format. The SDT contains some header like fields such as JMSDeliveryMode. Some of these fields are added by the API, some by the broker on message ingress. That may help with understanding but it doesn't answer your question.
So, how to extract the payload that you added? Well, knowing the message type (i.e. is this text, binary etc? See solace.MessageType ) will help, so you could call mesage.getType() to find out. How is the payload added to the message in your publisher?5 -
Hi TomF,
Thanks for your response. The payload in this case was generated by running a java program which generates ActiveMQ messages (kind of like JMS messages). I displayed the message type and it shows up as "0 - I think it means BINARY (the other types are MAP(1), STREAM(2) and TEXT(3). I 've tried something like message.getBinaryAttachment.toString(,xy) and message.getSdtContainer but they didn't get me what I want.
Any ideas?
Thanks,Eddie
0 -
@eddie Was the message from the publisher attached as a binary attachment (or did you do any modifications on the publisher side from the samples)? i.e.
message.setBinaryAttachment(PayloadMessageText);
You can check out a simpler demo usage here https://github.com/TamimiGitHub/SimpleSolace, this repo sets up a simple PubSub locally with a broker using a docker image that you can try it on your machine (simply run the docker-compose up command and follow the readme). In this example
message.getBinaryAttachment()
prints out the content of the message in text format. If you also attempted to access the SDT container fieldmessage.getSdtContainer()
you will see that its null0