A wrapper of amqp.node bundled as a sails js Hook that enables you to use the AMQP protocol for sending and receiving messages.
$ npm install sails-hook-amqp --save
Create a file named amqp.js in your config
directory with the following content
module.exports.amqp = {
amqpUrl: 'amqp://your-amqp-server-url',
active : true // Whether or not the hook will be active (defaults to true),
socketOptions : {
noDelay : true | false // boolean
cert: certificateAsBuffer, // client cert
key: privateKeyAsBuffer, // client key
passphrase: 'MySecretPassword', // passphrase for key
ca: [caCertAsBuffer] // array of trusted CA certs
}
};
The
socketOptionsproperty can be omitted if not relevant.
See amqp.node@connect for details.
Publish a single message to an exchange.
The [options] parameter can be omitted in favour of defaults.
defaultOpts = { persistent: true }
See amqp.node@channel_publish for details.
// JSON data
sails.hooks.amqp.publish("exchange-type-or-empty-string","my-routing-key")
// Simple text
sails.hooks.amqp.publish("exchange-type-or-empty-string","my-routing-key","Hello world!!")
// Buffer
sails.hooks.amqp.publish("exchange-type-or-empty-string","my-routing-key",new Buffer("Hello world!!"))
Send a single message with the content given as a buffer, string or JSON to the specific queue named, bypassing routing. The options and return value are exactly the same as for #publish.
The [options] parameter can be omitted in favour of defaults.
defaultOpts = { persistent: true }
See amqp.node@channel_sendToQueue for details.
sails.hooks.amqp.sendToQueue("my-queue-name",{
foo : "bar"
})
The [assertQueueOpts] & [consumeOpts] parameters can be omitted in favour of defaults.
defaultAssertQueueOpts = { durable: true }
defaultConsumeOpts = { noAck: false }
See amqp.node@channel_assertQueue, amqp.node@channel_consume for details.
sails.hooks.amqp.subscribe("my-queue-name",function onMessage(msg){
console.log(msg)
})
// Get Connection var connection = sails.hooks.amqp.getConnection(); // Get Publication Channel var pubChannel = sails.hooks.amqp.getPubChannel();
var amqp = sails.hooks.amqp.new();
amqp.connect("amqp://your-server-url",function (err,instance) {
if(err) {
console.log(err);
return;
}
instance.subscribe("my-routing-key",function onMessage(msg){
console.log(msg)
})
instance.subscribe("my-queue-name",function onMessage(msg){
console.log(msg)
})
instance.publish("exchange-type-or-empty-string","my-routing-key","Hello world!!")
instance.sendToQueue("my-queue-name", new Buffer("Hello world!!"))
})