Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ and Sails.js

I'm having trouble using RabbitMQ with my Sails app. I'm unsure of where to place the subscriber code. What I'm trying to do is build a notifications system so that when an administrator approves a user's data request, the user's dashboard will pop a notification similar to how Facebook pops a notification. The problem is, putting the subscriber code in my dashboard controller's display route seems to never grab a published message.

Any advice would be greatly appreciated. Currently using rabbit.js package to connect to RabbitMQ.

like image 990
kk415kk Avatar asked Aug 10 '14 18:08

kk415kk


1 Answers

To answer the original question, if one for some reason wanted to use Rabbit MQ instead of Sails' built-in resourceful pubsub, the best thing would be to use rabbit.js.

First, npm install rabbit.js.

Then, in your Sails project's config/sockets.js (borrowed liberally from the rabbit.js socket.io example):

var context = require('rabbit.js').createContext();
module.exports = {

     onConnect: function(session, socket) {
        var pub = context.socket('PUB');
        var sub = context.socket('SUB');

        socket.on('disconnect', function() {
          pub.close();
          sub.close();
        });

        // NB we have to adapt between the APIs
        sub.setEncoding('utf8');
        socket.on('message', function(msg) {
          pub.write(msg, 'utf8');
        });
        sub.on('data', function(msg) {
          socket.send(msg);
        });
        sub.connect('chat');
        pub.connect('chat');

    }

}
like image 60
sgress454 Avatar answered Oct 06 '22 01:10

sgress454