Skip to main content

amqpconnection.opensession(callback)

Attempts to open a new AMQP messaging session

Availability

Agent

Parameters

Name Type Description
callback Function An optional function that will be called at key moments during the life of the object

Returns

An amqpsession object

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 immediately returns a new amqpsession object and attempts to open the session. You should not assume the session is open when the method returns. Success or failure of the attempt to open the session is signalled to the callback registered with amqp.openconnection(), but code can call amqpsession.isopen() at any time to check the session status.

Any number of sessions may be opened on a single amqpconnection and each can comprise any number of senders and receivers through which messages can be transferred.

Passing a function into the callback parameter is optional. If provided, it should have the following parameters of its own:

Parameter Type Description
event String A constant representing the type of event that triggered the callback
errorDetail String or null In the event of an error, an error description, otherwise null

See amqp.openconnection() for details of these parameters and the values passed into them.

amqpsession objects are automatically closed when they are deallocated, which occurs (also automatically) when they go out of scope. To manually close an amqpsession, it must be deallocated by setting it to null.

Example

The following code shows how a session is opened following the receipt of a CONNECTION_OPEN event. The function amqpConnectionManager() has been already registered with amqp.openconnection() as the chosen connection event callback.

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);
    }
  }
}

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