Skip to content

A sails hook that uses the AMQP protocol for sending and receiving messages

License

Notifications You must be signed in to change notification settings

pantsel/sails-hook-amqp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sails-hook-amqp

A wrapper of amqp.node bundled as a sails js Hook that enables you to use the AMQP protocol for sending and receiving messages.

Installation

$ npm install sails-hook-amqp --save

Configuration

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 socketOptions property can be omitted if not relevant.

See amqp.node@connect for details.

Usage

Publish

Publish a single message to an exchange.

sails.hooks.amqp.publish(exchange, routingKey, content, [options])

The [options] parameter can be omitted in favour of defaults.

defaultOpts = { persistent: true }

See amqp.node@channel_publish for details.

Examples

// 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 to Queue

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.

sails.hooks.amqp.sendToQueue(queue, content, [options])

The [options] parameter can be omitted in favour of defaults.

defaultOpts = { persistent: true }

See amqp.node@channel_sendToQueue for details.

Examples

sails.hooks.amqp.sendToQueue("my-queue-name",{
    foo : "bar"
})


Subscribe

sails.hooks.amqp.subscribe([queue || routingKey],onMessageCallback,[assertQueueOpts],[consumeOpts])

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.

Example

sails.hooks.amqp.subscribe("my-queue-name",function onMessage(msg){
    console.log(msg)
})

Other methods

// Get Connection
var connection = sails.hooks.amqp.getConnection();

// Get Publication Channel
var pubChannel = sails.hooks.amqp.getPubChannel();


Connecting manually

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!!"))
})

About

A sails hook that uses the AMQP protocol for sending and receiving messages

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published