Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
fh-amqp-js
Advanced tools
FeedHenry AMQP client wrapper for communication with a Rabbit cluster.
Project Info | |
---|---|
License: | Apache License, Version 2.0 |
Build: | npm |
Documentation: | http://docs.feedhenry.com/v3/api/cloud_api.html |
Issue tracker: | https://issues.jboss.org/projects/FH/summary |
Mailing list: | feedhenry-dev (subscribe) |
IRC: | #feedhenry channel in the freenode network. |
The FeedHenry Platform includes a RabbitMQ cluster, which is set up to have highly available Mirrored Queues. The messaging pattern that best suits our development requirements is the 'Topic' pattern, described in the RabbitMQ documentation.
This client library helps with the following:
Automatic detection of Rabbit node failure and reconnecting to another node in the Rabbit Cluster. NOTE: all rabbitmq servers in the cluster need to be runnning on the same port, protocol and authentication method.
Easier handling connection and subscibers when RabbitMQ is down
'publishTopic' and 'subscribeToTopic' type functions that set up correct Exchange and Q configuration behind the scenes.
Option to cache messages that need to be published when RabbitMQ is down and re-publish them automatically when connection is backup.
If you need, you can also use 'getConnection' and 'getExchange' to create exchanges & queues that suit your needs.
This module can also be used from the command line, to quick publish a message to the FeedHenry Rabbit Cluster, or also as a handy way to quickly subscribe to FeedHenry messages.
cfg (Object) - Specify the AMQP connection details and other connection options.
cfg.clusterNodes
This is required. The clusterNodes can be either an array of amqp URIs, or the connection configuration object specified in https://github.com/postwait/node-amqp#connection-options-and-url. If the clusterNodes are an array of amqp URIs, they will be converted to node-amqp connection configuration. E.g.
cfg.options
You can specify node AMQP specific options that can be passed down to node-amqp (e.g. Reconnecting configurations), or if publish messages should be cached if connection is lost:
Establish the RabbitMQ connection
Get the current established RabbitMQ connection. It's an instance of the node-amqp Connection.
Get/create an exchange with the given name and options.
Publish a topic message to the exchange. If 'cachePublish' is set to true in cfg.options when the AMQPManager is constructed, the messages will be cached in memory if RabbitMQ is down and then re-published automatically once the connection is back. You can also control the maxium number of messages to cache via 'maxCachePublishSize' in cfg.options. This feature is off by default, and the default maxium number of messages to cache is 1000.
Subscribe to a topic. Even if the connection is down at the time when this function called, the AMQPManager will automatically add the subscriber once the connection is restored. So you don't need to care about the status of the connection when this method is called.
Quickly check if there is a valid RabbitMQ connection before publishing/subscribing to a exchange/queue. This could be useful if the application needs to make sure the message bus is alive before pub/sub.
Disconnect from the message queue.
The following events will be emmitted by the AMQPManager:
This event is emmitted EVERYTIME when the connection is established.
This event is only emmitted the very first time when connection to the RabbitMQ is established since the component is started.
This event is emmitted when there is a reconnect happened.
This event is emmitted when there is an error. The error object will be passed to the listener. Normally in your application, you should just log the error and do not try to reconnect or stop the service. The AMQPManager will try to reconnect automatically. Even if your application should not be running when RabbitMQ is down, you should probably count the number of errors (or add a delay) before you decide to stop the application.
The AMQPManager also inherits from the EventEmitter, so all the methods of EventEmitter are also available.
Sample sub.js:
var fhamqpjs = require('../lib/amqpjs.js');
var cfg = {
clusterNodes: "amqp://guest:guest@dummy.feedhenry.me:5672",
maxReconnectAttempts: 10 // specifies how may connection failures to attempt before giving up.
};
var amqpManager = new fhamqpjs.AMQPManager(cfg);
amqpManager.connectToCluster();
//just subscribe. Even if RabbitMQ is down, this subscriber will be added automatically once RabbitMQ is back.
amqpManager.subscribeToTopic("fh-topic1", "fh-topic-1", "fh.#", subscribeFunc, function(err){
if(err) console.error("Fatal error setting up subscriber: ", err);
});
// error handler: just log. Do not try to reconnect or quit.
amqpManager.on("error", function(err){
console.log("Fatal error: ", err);
});
// the function that gets called each time a message is recieved
function subscribeFunc (json, headers, deliveryInfo) {
console.log("GOT MESSAGE: ", json);
};
Sample pub.js:
var fhamqpjs = require('../lib/amqpjs.js');
var cfg = {
clusterNodes: "amqp://guest:guest@dummy.feedhenry.me:5672",
maxReconnectAttempts: 10,
options: {
cachePublish: true
}
};
var amqpManager = new fhamqpjs.AMQPManager(cfg);
amqpManager.connectToCluster();
var t; var count=0;
if (t) clearInterval(t);
t = setInterval(function(){
console.log("Publishing message: " + count);
amqpManager.publishTopic("fh-topic1", "fh.event.count", {count: count++}, function(err){
if (err) console.error("Fatal publishing error: ", err);
});
}, 1000);
amqpManager.on("error", function(err){
console.log("Fatal error: ", err);
});
Since release 0.3, this module will support reconnecting to the rabbitmq servers if it restarts. If the current connected rabbit server is disconnected for whatever reason, this module will automatically try to connect to another host (if there are multiple hosts specified) or the same host (if only one is specified) based on reconnect strategy. It enables the reconnect function that is supported by the node-amqp module: https://github.com/postwait/node-amqp#connection-options-and-url.
The default reconnect options are:
{
reconnect: true,
reconnectBackoffStrategy: 'linear',
reconnectBackoffTime: 5000
}
To override, you can set an options
field on the configuration object passed to the AMQPManager:
var cfg = {
clusterNodes: ["amqp://guest:guest@dummy.feedhenry.me:5672/fh-events"],
options: {
reconnectBackoffStrategy: 'exponential',
reconnectExponentialLimit: 120000
}
};
You can also set other options that is supported by node-amqp module in the options
field.
You can also specify the clusterNodes
option as connection options that is supported by node-amqp:
var cfg = {
clusterNodes: {
host:['rabbitserver1.com', 'rabbitserver2.com'],
port: 5672,
login: 'guest',
password: 'guest'
}
}
var amqpManager = new fhamqpjs.AMQPManager(cfg);
amqpManager.connectToCluster();
NOTE: all rabbitmq servers in the cluster need to be runnning on the same port, protocol and authentication method.
Usage: fh-amqp-js pub <exchange> <topic> <message> --clusterNodes=[<amqp-url>,*]
fh-amqp-js sub <exchange> <topic> --clusterNodes=[<amqp-url>,*]
The Command Line Interface can be used to quickly publish messages, e.g.
$ fh-amqp-js pub "fh-topic2" "fh.event.count" '{"count": 1}' --clusterNodes='["amqp://guest:guest@dummy.feedhenry.me:5672"]'
There is also a 'sub' command, for quickly subscribing to messages:
$ fh-amqp-js sub "fh-topic2" "fh.event.count" --clusterNodes='["amqp://guest:guest@dummy.feedhenry.me:5672"]'
Configuration:
The CLI uses the RC node module for incredibly flexible config finding (see its documentation). Config options currently are:
{
clusterNodes: ["amqp://guest:guest@dummy.feedhenry.me:5672/fh-events"],
maxReconnectAttempts: 10 // specifies how may connection failures to attempt before giving up.
}
To run the tests:
make test
or:
make test-coverage-cli
or:
make test-coverage-html
Build artifacts are located on Denzil here: http://denzil.henora.net:8080/view/common/job/fh-amqp-js/
FAQs
FeedHenry AMQP Client
The npm package fh-amqp-js receives a total of 4 weekly downloads. As such, fh-amqp-js popularity was classified as not popular.
We found that fh-amqp-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 7 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.