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.

kk415kk
  • 1,227
  • 1
  • 14
  • 30
  • Have you considered using Sails' [build in resourceful pubsub system](http://sailsjs.org/#/documentation/reference/websockets/resourceful-pubsub) for messaging? – sgress454 Aug 10 '14 at 19:35
  • 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? – kk415kk Aug 10 '14 at 21:04
  • This code lives in the front end of your app, in the client-side Javascript. It can go anywhere after the ` – sgress454 Aug 10 '14 at 21:23
  • 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? – kk415kk Aug 10 '14 at 21:28
  • 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`](http://sailsjs.org/#/documentation/reference/websockets/resourceful-pubsub/publishUpdate.html). Then in your view, `io.socket.on("request", function()...)` is correct. – sgress454 Aug 10 '14 at 21:35
  • I made a new question about this [link](http://stackoverflow.com/questions/25233329/sails-js-subscribing-a-user-to-specific-actions-on-a-request), since it's getting off-topic from the original question - thanks! – kk415kk Aug 10 '14 at 21:47
  • check out this example project of using Sails websockets, perhaps it will help you understand hw everything fits together https://github.com/stenio123/sails-socket-example – Stenio Ferreira May 19 '15 at 13:45

2 Answers2

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

    }

}
sgress454
  • 24,870
  • 4
  • 74
  • 92
0

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

Cahlan Sharp
  • 793
  • 7
  • 9