Skip to main content

amqpdelivery.accept()

Informs the AMQP broker you have accepted the amqpdelivery’s message

Availability

Agent

Returns

Nothing

Description

Important Note Agent AMQP functionality is now deprecated and will shortly become unsupported. Any attempt to interact with imp API amqp objects and methods on unregistered development devices or on production devices will generate a runtime error.

If you are using or intend to use Azure IoTHub, we recommend you make use of MQTT instead of AMQP. Please see our Azure IoT Hub integration for more information.


This method informs the AMQP broker that you have accepted delivery of the target amqpdelivery object’s message.

Example

The following code shows how a receiver is opened following the receipt of a SESSION_OPEN event. With the session open, the code is safe to create an amqpreceiver object and register the callback that will process incoming messages: amqpMessageManager(), which simply logs the messages embedded in received amqpdelivery objects, and accepts each delivery.

const HUB_NAME = "<YOUR_HUB_NAME>";

conn <- null;
session <- null;
receiver <- null;

function amqpConnectionManager(event, errorDetail) {
  if (errorDetail) {
    server.error(errorDetail);
  } else {
    // Received an event from the AMQP connection
    switch(event) {
      case "CONNECTION_OPEN":
        // The connection is now open - create a session
        session = conn.opensession(amqpConnectionManager);
        break;

      case "SESSION_OPEN":
        // The session is now ready, create a receiver
        if (session) receiver = session.openreceiver("$cbs",
                                                     amqpConnectionManager,
                                                     amqpMessageManager);
    }
  }
}


function amqpMessageManager(deliveries) {
  // Process recieved amqpdelivery objects
  server.log(format("Received %d responses from Azure", deliveries.len()));
  if (deliveries.len() > 0) {
    foreach (delivery in deliveries) {
      local msg = delivery.message().body();
      delivery.accept();
      server.log(msg);
      if (delivery.message().properties().status-code == 200) server.log("Authorized with Azure IoT Hub");
    }
  }
}

// Initiate the connection
local url = "amqps://" + HUB_NAME + ".azure-devices.net:5671";
conn = amqp.openconnection(url, amqpConnectionManager);