Skip to content

Posting messages to a queue

ts
function PostMessage(queueName: string, message: string): boolean;

PostMessage() publishes a plain-text message to the named queue on the connected AMQP broker. It returns true if the message was delivered successfully, or false if an error occurred.

NOTE

PostMessage() works the same way on both AmqpClient091 and AmqpClient10 objects.

Implementation notes

ClientBehaviour
AmqpClient091Uses the default exchange with queueName as the routing key. The queue is declared automatically if it does not yet exist.
AmqpClient10Opens a sender link to the target address, sends the message, then closes the sender. This is safe and correct for all AMQP 1.0 brokers.

Example: post a message and disconnect

ts
ConsoleFeedback = true;
var cli = new AmqpClient091();
cli.URL = 'amqp://localhost:5672';
cli.User = 'guest';
cli.Pass = 'guest';
if (cli.Connect()) {
  var ok = cli.PostMessage('myqueue', 'Hello from SyncJS!');
  Log('Posted: ' + ok);
  cli.Close();
}
cli = null;

Example: post and monitor in the same session

You can call both MonitorQueue() and PostMessage() on the same connected client object.

ts
ConsoleFeedback = true;
var cli = new AmqpClient091();
cli.URL = 'amqp://localhost:5672';
cli.User = 'guest';
cli.Pass = 'guest';
if (cli.Connect()) {
  cli.MonitorQueue('responses');
  cli.PostMessage('requests', JSON.stringify({ action: 'ping', ts: Date.now() }));
  Sleep(3000);
  var msgs = cli.GetMessages();
  Log(JSON.stringify(msgs));
  cli.Close();
}
cli = null;