Subscribe to ActiveMQ using nodejs

Hi,

I have a use case where we need to listen to activemq queue and based on the event , do some processing. For this I have a nodejs app deployed in apigee edge.

I am using a stomp-client npm module which connects to activemq deployed at localhost

But I am not able to connect to activemq. Am I missing something ?

var Stomp = require('stomp-client');
var http = require('http');
var request = require('request');
var destination = '/queue/someQueueName';
var express = require('express');
var app = express();

var client = new Stomp('127.0.0.1', 61613, 'username', 'password');
	
client.connect(function(sessionId) {
console.log("in connect.............");
 
 client.subscribe(destination, function(body, headers) {
      console.log('This is the body of a message on the subscribed queue:', body);
     
    });
});




																	
0 11 3,095
11 REPLIES 11

Not applicable

There are a few things to consider about your code:

  • You're doing the right steps. Test locally, deploy in the cloud. However, I assume that you'll parameterize your code to run the Express app to use a public IP address.
  • If you're running Apigee Edge Cloud, your ActiveMQ should be reachable through a public IP. Loopback IP address would point to the MP, which for public cloud is disabled to run third-party services. My suggestion if you want to look into getting an instance of ActiveMQ running with a public IP address, you may need a GCE (Google Compute Engine) micro and install this package.
  • Express is being included but not used. I assume this code is for testing/learning purposes. However, if the intent of your API is to push the response of a subscription, I'd suggest registering endpoints in Express. Such as: app.get('/endpoint', callback).

Where is your activemq running? The code you shared would be trying to connect to activemq running where edge is running. You're using Edge OPDK running locally?

Former Community Member
Not applicable

I also think this pattern is not suited to run in Apigee.

Apigee is suited to patterns where your node.js application exposes HTTP and put that message on a queue or topic. The trigger for your node.js application is still the API proxy (which only exposes http).

@Diego Zuluaga We have an on premise setup and would like to connect to ActiveMQ within our corporate network. We have an ActiveMQ instance running in the corp network, but before that I would like to test it locally. i.e (ActiveMQ deployed at localhost)

But am not able to connect to ActiveMW at localhost using the above code. Is this possible to achieve ?

Thanks

Also I get the below error taken from the logs. The connection is being refused, but not able to figure out the reason.

 Error: connect ECONNREFUSED Error: connect ECONNREFUSED at emit (events.js:72) at /organization/environment/api/node_modules/stomp-client/lib/client.js:145 at emit (events.js:95) at net.js:440 at _tickDomainCallback (trireme.js:491) at _tickFromSpinner (trireme.js:422)

You are running the above posted code in Edge deployed to your laptop as AIO profile?

Does the code run successfully outside of Edge?

The above code works outside of Edge. The issue got resolved after I changed localhost to the actual IP of my machine. Not sure why using localhost to connect to ActiveMQ was being denied.

Possibly due to restrictions on loopback from the MP. I believe that may not be allowed.

However, Nandan's response above is worth thinking about. Generally the node.js capability is designed to handle incoming HTTP requests, not create a long-running process that responds to events that don't come through the proxy. You may find that using a more traditional approach for running node.js code is easier to debug, maintain, etc.

Thanks for clarifying. Using 127.0.0.1 IP address on your laptop with plain vanilla Node.js should work. If it doesn't, there's no point on uploading the code to Edge.

My suggestion would be first, try to figure out what's preventing you from reaching out ActiveMQ service. Then once you find out what's missing, proceed to deploy it to Edge making sure ActiveMQ server is accessible from the MP.


Once your local tests work. Proceed to deploy to deploy to Edge. And if you run into connectivity issues. There might be a need to whitelist some IPs.

I read somewhere that you need to whitelist private CIDRs (local IP addreses). @Hansel Miranda I believe you bumped into this issue. Do you recall what file needs to be modified to whitelist these IPs?

The issue was resolved after using the actual IP of my machine.