Skip to main content

amqpsession.isopen()

Indicates whether the target session is open or closed

Availability

Agent

Returns

Boolean — true if the amqpsession is open, otherwise false

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 is used to check whether the amqpsession object represents an open or a closed session. If the session is open, the call returns true. The call can be used check on the state of the session before executing code that depends on the session being open and will fail if it is not.

Example

The following code shows how an open session is closed after a ten-minute delay by calling close() — after first checking that the session is still open by using isopen().

const HUB_NAME = "<YOUR_HUB_NAME>";

conn <- null;
session <- null;

function amqpConnectionManager(event, errorDetail) {
  // Received an event from the AMQP connection
  if (errorDetail) {
    server.error(errorDetail);
  } else {
    switch(event) {
      case "CONNECTION_OPEN":
        session = conn.opensession(amqpConnectionManager);

        // Close session after 10 minutes if it's not already
        imp.wakeup(600, function() {
          if (session.isopen()) {
            session.close();
            session = null;
          }
        });
    }
  }
}

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