Skip to main content

amqpsession.opensender(path, callback)

Establishes a means to send AMQP messages

Availability

Agent

Parameters

Name Type Description
path String The path of the target endpoint relative to the host
callback Function An optional function that will be called at key moments during the life of the object

Returns

An amqpsender 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 creates and returns a new amqpsender object which represents a pipe to the AMQP broker at the endpoint specified by path. The sender is prepared for use asynchronously and may not be ready when opensender() returns. Success or failure of the attempt to ready the sender is signalled to the callback registered with amqp.openconnection(), but code can call amqpsender.isopen() at any time to check the sender status.

Passing a function into the callback parameter is optional. If a callback function is 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.

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

Example

The following code shows how a sender is opened following the receipt of a SESSION_OPEN event. The function amqpConnectionManager() has been already registered with amqp.openconnection() as the chosen connection callback. When the same callback subsequently receives a SENDER_OPEN message, it can initiate the transfer of messages via an amqptransfer object.

const HUB_NAME = "<YOUR_HUB_NAME>";

conn <- null;
session <- null;
sender <- 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);
        break;

      case "SESSION_OPEN":
        if (session) sender = session.opensender("$cbs", amqpConnectionManager);
    }
  }
}

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