RabbitMQ and Sails.js
Asked Answered
L

2

6

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.

Lysin answered 10/8, 2014 at 18:19 Comment(7)
Have you considered using Sails' build in resourceful pubsub system for messaging?Ronironica
Thanks sgress454! This is definitely what I'm looking for. I still have a similar question though: for receivers, where exactly does the "listening" code go? For example, using io.socket.on("request", function()...), where would I place this code so that an user can continuously listen for an update when a "request" has been approved for that specific user?Lysin
This code lives in the front end of your app, in the client-side Javascript. It can go anywhere after the <script> tag that includes the Sails socket client,or in a bootstrapping script like jQuery's $(function(){}).Ronironica
So, in my RequestController, under the grant action, I publish an update when it's executed. In my DashboardController, under my display action, I make a call to Request.find(req.session.user.id, function(err, requests) and I subscribe using User.subscribe(req.socket, requests, ['update']);. Is this the correct way to place publish/subs? On the view that DashboardController.display renders, do I just need to use io.socket.on("request", function()...) to listen for the publishes?Lysin
In your DashboardController, you would subscribe using Request.subscribe(req, requests, ['update']), assuming you only want to hear about updates (and not deletes). In your RequestController, publish the update using .publishUpdate. Then in your view, io.socket.on("request", function()...) is correct.Ronironica
I made a new question about this link, since it's getting off-topic from the original question - thanks!Lysin
check out this example project of using Sails websockets, perhaps it will help you understand hw everything fits together github.com/stenio123/sails-socket-exampleSoracco
R
5

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');

    }

}
Ronironica answered 11/8, 2014 at 3:27 Comment(0)
L
0

Here's an npm package that implements a RabbitMQ adapter for SailsJS.

Lieutenant answered 10/9, 2015 at 22:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.