eclipse-ditto/ditto-clients

[JS] Responding to messages

emcellsoft opened this issue · 1 comments

I'm not sure if I'm doing something wrong, or if this is a problem in the api.

I'm trying to respond to a message that is sent to a thing.

As the documentation states out here, I have to set the correlation-id header to the same value as the message that I've received.

I currently do not have chance to get the correlation-id of the message.

  const client = DittoNodeClient.newWebSocketClient()
    .withTls()
    .withDomain(domain)
    .withAuthProvider(NodeHttpBasicAuth.newInstance(username, password))
    .withBuffer(1024 * 10)
    .liveChannel()
    .build();

  const messages = client.getMessagesHandle();
  await messages.requestMessages();
  messages.subscribeToThing(thingId, (msg) => {
    console.log(msg.headers); // always undefined
    console.log(msg);
  });

In my example the msg.headers attribute is always undefined. I've had a look into the code and the headers are present but will not get copied into the message:

websocket-request-handler.js:174-196

/**
 * A subscription to messages matching a certain pattern.
 */
class Subscription {
    constructor(_callback) {
        this._callback = _callback;
    }
    /**
     * Calls the callback function with the message provided.
     *
     * @param message - The message to send.
     */
    callback(message) {
        const splittedTopic = message.topic.split('/');
        const action = splittedTopic.length > 0 ? splittedTopic.pop() : '';
        this._callback({
            action,
            topic: message.topic,
            path: message.path,
            value: message.value
        });
    }
}

The debugger states out that the headers are present in message but won't get forwarded to _callback.

Do I need another api to answer messages?

@emcellsoft that looks indeed like a bug - the headers from the passed in message would need to be passed into the callback.

We would happily accept a PR fixing this:
https://github.com/eclipse/ditto-clients/blob/2e9bb2a5319d98269abde20424491d18db0d6256/javascript/lib/api/src/client/request-factory/websocket-request-handler.ts#L244-L249

For me, it looks also strange that the headers of the ProtocolResponseValue are defined as type string:
https://github.com/eclipse/ditto-clients/blob/2e9bb2a5319d98269abde20424491d18db0d6256/javascript/lib/api/src/client/request-factory/websocket-request-handler.ts#L317

I think that should be of type { [key: string]: any } instead.

And - as I understand the ProtocolResponseValue defines the signature (topic, path, action, headers?, value?).
However this passes in the arguments in another order: (action, topic, path, value):
https://github.com/eclipse/ditto-clients/blob/2e9bb2a5319d98269abde20424491d18db0d6256/javascript/lib/api/src/client/request-factory/websocket-request-handler.ts#L244-L249

So there seem to be some bugs when it comes to sending responses with the JS client.
So as I said: We would happily accept a PR fixing this 👍