Persistence with Queues

Persistence with Queues

This tutorial will show you how to send and receive persistent messages with Node.js using the amqp10 AMQP 1.0 compliant client and Solace messaging

At the end, this tutorial walks through downloading and running the sample from source.

Assumptions

This tutorial assumes the following:

  • You are familiar with Solace core concepts.
  • You have access to Solace messaging with the following configuration details:
    • Connectivity information for a Solace message-VPN configured for guaranteed messaging support
    • Enabled client username and password
    • Client-profile enabled with guaranteed messaging permissions.

One simple way to get access to Solace messaging quickly is to create a messaging service in Solace Cloud as outlined here. You can find other ways to get access to Solace messaging below.

Goals

The goal of this tutorial is to demonstrate how to use the amqp10 AMQP 1.0 compliant client with Solace messaging. This tutorial will show you:

  1. How to send a persistent message to a durable queue with Solace messaging
  2. How to bind to this queue and receive a persistent message

Get Solace Messaging

This tutorial requires access Solace messaging and requires that you know several connectivity properties about your Solace messaging. Specifically you need to know the following:

Resource Value Description
Host String This is the address clients use when connecting to the Solace messaging to send and receive messages. (Format: DNS_NAME:Port or IP:Port)
Client Username String The client username. (See Notes below)
Client Password String The client password. (See Notes below)

There are several ways you can get access to Solace Messaging and find these required properties.

Option 1: Use Solace Cloud

  • Follow these instructions to quickly spin up a cloud-based Solace messaging service for your applications.
  • The messaging connectivity information is found in the service details in the connectivity tab (shown below). You will need:
    • Host:Port (use the AMQP URI)
    • Client Username
    • Client Password

Option 2: Start a Solace VMR

  • Follow these instructions to start the Solace VMR in leading Clouds, Container Platforms or Hypervisors. The tutorials outline where to download and how to install the Solace VMR.
  • The messaging connectivity information are the following:
    • Host: <public_ip> (IP address assigned to the VMR in tutorial instructions)
    • Client Username: sampleUser (can be any value)
    • Client Password: samplePassword (can be any value)

    Note: By default, the Solace VMR “default” message VPN has authentication disabled.

Option 3: Get access to a Solace appliance

  • Contact your Solace appliance administrators and obtain the following:
    • A Solace Message-VPN where you can produce and consume direct and persistent messages
    • The host name or IP address of the Solace appliance hosting your Message-VPN
    • A username and password to access the Solace appliance

AMQP 1.0 compliant client

The amqp10 AMQP 1.0 compliant client is an open-source JavaScript client for sending and receiving messages over AMQP 1.0. If you choose to use a different AMQP 1.0 compliant client, adjust the given in this tutorial examples accordingly.

Obtaining amqp10

This tutorial depends on you having the amqp10 AMQP 1.0 compliant client downloaded and installed for your project, and the instructions in this tutorial assume you successfully done it. If your environment differs then adjust the build instructions appropriately.

The easiest way to do it through npm:

$ npm install amqp10 -save

Connecting to the Solace Messaging

The amqp10 client uses Promise from the Bluebird library that is a superset of the ES6 Promise specification, but our tutorial examples will follow only the ES6 Promise specification.

In order to send or receive messages, an application that uses the amqp10 client must start a connection to the Solace messaging AMQP service URL. The URL consists of the Solace username, password, and host name with the AMQP service port number in the form amqp://<username>:<password>@<host:port>

Assigning defaultSubjects to false allows the use of a slash-separated hierarchy in the queue name.

QueueProducer.js/QueueRecevier.js

var AMQP = require('amqp10');

self.host = function(hostname) {
    self.hostname = hostname;
    return self;
};

var amqpClient = new AMQP.Client(AMQP.Policy.merge({
    defaultSubjects : false
}));

...
    amqpClient.connect(self.hostname).then(() => {
...

At this point the application is connected to Solace messaging and ready to send and receive messages.

Sending a persistent message to a queue

In order to send a message to a queue a Sender needs to be created.

sending-message-to-queue

The name of the queue for sending messages is given to Sender when it is being created.

QueueProducer.js

amqpClient.connect(self.hostname).then(()
    return amqpClient.createSender(self.queueName);
}).then((amqpSender) => {
    return amqpSender.send(message).then(() => {

Receiving a persistent message from a queue

To receive a persistent message from a queue a Receiver needs to be created.

The name of the queue for sending messages is given to Receiver when it is being created and it is the same as the one we send messages to.

The created Receiver emits events, and listener functions for at least message and errorReceived events need to be declared. A message event is emitted for every message recevied by the Recevier.

QueueConsumer.java

amqpClient.connect(self.hostname).then(() => {
    return amqpClient.createReceiver(self.queueName);
}).then((amqpReceiver) => {
    amqpReceiver.on('message', (message) => {
        ...
    });
    amqpReceiver.on('errorReceived', (error) => {
        ...
    });

Summary

Combining the example source code shown above results in the following source code files:

Getting the Source

Clone the GitHub repository containing the Solace samples.

git clone https://github.com/SolaceSamples/solace-samples-amqp-nodejs
cd solace-samples-amqp-nodejs

Running

The examples can be executed as:

node src/QueueConsumer.js amqp://<username>:<password>@<host:port>
node src.QueueProducer.js amqp://<username>:<password>@<host:port>

Sample Output

Start the QueueConsumer so that it is up and waiting for messages.

$ node src/QueueConsumer.js amqp://<username>:<password>@<host:port>
[17:13:14] Connecting to amqp://<username>:<password>@<host:port>
[17:13:14] Waiting for messages...

Then run the QueueProducer to send the message.

$ node src/QueueProducer.js
[17:13:53] Connecting to amqp://<username>:<password>@<host:port>
[17:13:53] Sending message 'Hello world Queues!'...
[17:13:53] Message sent successfully.
[17:13:55] Finished.

Notice how the message is received by the QueueConsumer.

...
[17:13:14] Waiting for messages...
[17:13:53] Received message: 'Hello world Queues!'.
[17:13:55] Finished.

Now you know how to use the amqp10 AMQP 1.0 compliant Node.js client with Solace messaging to send and receive persistent messages from a queue.

If you have any issues sending and receiving message or reply, check the Solace community for answers to common issues seen.