Skip to main content

amqpsender.createtransfer(message)

Establish a single transfer to transport an outgoing AMQP message

Availability

Agent

Parameters

Name Type Description
message An amqpmessage object The AMQP message to be sent

Returns

An amqptransfer object

Description

Important Note Agent AMQP functionality is currently only available to new devices’ agents upon request. Any attempt to interact with imp API amqp objects and methods on un-requested development or on production devices will generate a runtime error.


This method creates a single AMQP transfer that can be used to send the specified AMQP message.

Each attempt to send (or resend) a message requires the creation of a new transfer object. Once the message has been sent, or the transfer cancelled, the amqptransfer object is removed.

Transfers will be queued until they are able to be sent by the sender. When this happens is entirely in the hands of the receiving end of the connection, which specifies the ‘credit’ (ie. the number of messages) that it can receive. When a message is sent to a receiver, the ‘credit’ will reduce by one until that message has been processed by the receiver.

Example

The following code shows how a transfer is initiated following the receipt of a SENDER_OPEN event. The function amqpConnectionManager() has been already registered with amqp.openconnection() as the chosen connection event callback. When this callback receives a SENDER_OPEN event — having already been triggered by the arrival of CONNECTION_OPEN and then SESSION_OPEN events — it can initiate the transfer of messages via an amqptransfer object instantiated using createtransfer().

const HUB_NAME = "<YOUR_HUB_NAME>";

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

      case "SENDER_OPEN":
        if (sender) {
          transfer = sender.createtransfer(amqp.createmessage("Hello!"));
          transfer.sendasync(function() {
            server.log("Message transferred");
          });
        }
    }
  }
}

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