Can I use the callback in the publish function to receive a result from the subscriber?

30 views Asked by At

I've written an AWS lambda function that uses IoT to publish messages that are subscribed to by remote devices. Each device only subscribes to messages containing its specific id. This is working well to publish messages to instruct the devices; however, I have a message to request information from the device. The publish function contains a callback which seems to be called, as I'm getting an entry in the log file, but I haven't been able to identify where in the subscriber I can populate the result value, if at all. It is always returned as undefined. This is the relevant code in the lambda function:

iot.device.publish(`pi/${device}/${event.body.actionType}`, payload,null,(err,result) => {
    util.log(`In publish callback. err: ${err} result: ${result}`)
    if (device === event.body.devices[event.body.devices.length - 1]) {
        published = true
    }
    if(err) {
        util.log('Failure:',err.message)
    }
})

I did not write the device code, but this appears to be where the message is being processed

device.on('message', (topic, data) => {
  let payload = parsePayload(data)
  logger.info(`received iot message for topic ${topic} with payload ${payload}`)

as I see that log entry in the device's log when the lambda function is executed. However, when I extend the parameter list, callback is undefined

device.on('message', (topic, data, nullObject, callback) => {
  let payload = parsePayload(data)
  logger.info(`received iot message for topic ${topic} with payload ${payload} nullObject ${typeof nullObject} callback ${typeof callback}`)

I'm stumped where in the subscriber the publisher's callback is being invoked and how to access it. Any insights or explanation of whether this is a valid approach, are greatly appreciated.

2024-02-23 Update: It appears the callback only takes an error parameter, so it is only useful for knowing a subscriber has consumed the message and testing for the absence / presence of an error. I will look into having the subscriber publish a message for the original publisher and have the publisher block / timeout, looking for the result from the original message.

2024-02-27 Update: I was able to leverage a colleague's code that wraps a publish in a promise that pushes a function onto a queue that gets popped off in the publisher's on message handler if it receives a response message from the subscriber. The function will cancel a timeout in the promise that rejects with "response not received" and instead resolves with the subscribers return details.

1

There are 1 answers

0
Ed Trembicki-Guy On

Issue resolved. A publish cannot receive a reply from the subscriber in the callback function; however, a framework can be developed to wait for the subscriber to reply with its own publish back to the original publisher that can be received by the publisher's on message event handler or time out on no return message within a selected amount of time.