How to get back the message id after publishing a message on solace topic using nodejs

solacenewbie80
solacenewbie80 Member Posts: 3
edited April 2023 in PubSub+ Event Broker #1

hi Members,

i need help to get the message id immediately after publishing a message on topic using nodejs. i can publish a message but not sure how to get the message id back after publishing.

i tried to search on the google but no luck.

here is the code i am using to publish.

code block :

const solace = require('solclientjs');
const hostUrl = 'url';
const vpnName = 'VPN';
const userName = 'user';
const password = 'pwd';
const topicName = 'testtopic';
const messagePayload = 'hello nodejs';

const factoryProps = new solace.SolclientFactoryProperties();

solace.SolclientFactory.init(factoryProps);

const session = solace.SolclientFactory.createSession({
 url: hostUrl,
 vpnName: vpnName,
 userName: userName,
 password: password
});

session.connect();

session.on(solace.SessionEventCode.UP_NOTICE, () => {
 console.log('Session is up.');

 const message = solace.SolclientFactory.createMessage();
 message.setDestination(solace.SolclientFactory.createTopicDestination(topicName));
 message.setBinaryAttachment(messagePayload);
message.setDeliveryMode(solace.MessageDeliveryModeType.PERSISTENT);
message.setAcknowledgeImmediately(true);	


 
 session.send(message);
 console.log(`Message sent: ${messagePayload}`);

 session.disconnect();
});

session.on(solace.SessionEventCode.DISCONNECTED, () => {
 console.log('Session is disconnected.');
});

session.on(solace.SessionEventCode.RECONNECTING_NOTICE, () => {
 console.log('Session is reconnecting.');
});

session.on(solace.SessionEventCode.RECONNECTED_NOTICE, () => {
 console.log('Session is reconnected.');
});

session.on(solace.SessionEventCode.CONNECT_FAILED_ERROR, (sessionEvent) => {
 console.error(`Error connecting to Solace message broker: ${sessionEvent.infoStr}`);
});


*************


thanks

Tagged:

Answers

  • arih
    arih Member, Employee Posts: 125 Solace Employee

    hi @solacenewbie80

    I don't think it is there in JS API yet, but I'll let others correct me if I'm wrong later :)

    But what is your requirement?

    I know for some use cases, we can work with something like correlation ID though, but this is something the publisher planted in the message, not the generated/assigned message ID by the broker.

    Interested to see what others can suggest too.

  • Aaron
    Aaron Member, Administrator, Moderator, Employee Posts: 636 admin

    Agree, you can't really. There is an associated message ID that the publisher might be able to see after publishing, but this is an internal number between the API and broker specifically for this particular ingress flow, and isn't / won't be the same number that the broker uses internally for storing in the message spool.

    What are you trying to do? What's the use case?

  • solacenewbie80
    solacenewbie80 Member Posts: 3

    @Aaron @arih , Thanks for the reply. use case is quite simple that we need to publish a message, using nodejs, and immediately requires a message id in response to keep track of what message was published. This is working as of now if we publish using the JMS to solace.

  • arih
    arih Member, Employee Posts: 125 Solace Employee

    Hi @solacenewbie80

    Yeah I remember JMS send method return the message ID from the broker, but bummer if you'd want to have this in JS API :(

    If you use Direct messaging, it's basically fire and forget, at least once delivery, but you can still track the published messages only from the app publisher point of view - just without a 'broker generated ID'.

    But if I understand this correctly, you want to track messages succesfully sent to the broker, right?

    In that sense, maybe @Aaron and others who know better can give samples around using ACK and guaranteed messaging to really make sure the broker gets your message, but then again it may not necessarily have the 'broker generated ID' but you can be sure that message is received.

  • solacenewbie80
    solacenewbie80 Member Posts: 3

    thanks @arih , yes you are right it wont be broker provided message id as per my understanding. may be others can suggest if any way of getting that. only way i could figure out is to use the method message.setApplicationMessageId which will bet set as a message id in payload.

  • Aaron
    Aaron Member, Administrator, Moderator, Employee Posts: 636 admin

    Ok, I had to go dig up some JavaScript samples and some JMS samples to test this stuff out myself.

    First: the message ID that appears to auto-populate with the JMS API is also known as the Application Message ID in our native SMF APIs. This is set by the app, or the API, and appears to be set automatically with JMS. The value I see:

    JMSMessageID:                           ID:172.31.233.989ffb187d91d9a150:0
    AppMessageID:                           ID:172.31.233.989ffb187d91d9a150:0
    

    I'm not sure how/where this is getting set. In fact, I can't seem to override it with message.setJMSMessageID("blah"); which is a bit weird. I'm going to ask about that.

    But anyhow, this is not an ID that is set by the broker. There are other IDs (the message spool ID you can see inside PubSub+ Manager listing of messages inside the queue, and also the Replication Group Message ID).

    So yes, the exact equivalent of this particular message header (JMS Message ID), is the "Application Message ID" in other Solace APIs. This is an application-owned/set metadata on the message that the broker doesn't use. And it is our recommendation that for any ID that the application requires for end-to-end tracking, this one is a good choice.

    Note: this is not inside the payload. It's a header field.

    Finally: if you're using JMS: I'd urge you to double-check that your publishers are not sending TextMessages using the default XML portion of the payload. This is the default configuration, and is due to legacy reasons, but it is very much not the recommended way. Double-check your connection factories.

  • Aaron
    Aaron Member, Administrator, Moderator, Employee Posts: 636 admin

    Hi @solacenewbie80 , just a quick follow-up here. Yes, the JMS Message ID is auto-populated by the API, and is not settable by the application. The format of it is set by the API, and there are apparently some options: https://docs.solace.com/API/Solace-JMS-API/Data-Connection-Properti.htm#Message

    The main thing is: it's not set by the broker.

    So you can emulate the same behaviour in other non-JMS APIs... just set it yourself... as you noted, the Application Message ID is the same field. So populate it with any unique-type value that you wish.

    However, note that this value won't be visible in the broker... you'll have to consume the message to be able to see it. So I'm still wondering: what are you using this message ID for? Just to have "some ID #" for a message? Or do you need some kind of traceability ID for your published messages?