2

I saw several example of chat functionality on sails.js. However, all of them create one big chat where all users are placed.

How to create multiple chats (so messages posted in one chat isn't visible in another).

I am not sure that I understand the problem correctly. It looks like what happen is that client does io.socket.post('/message', { someMessageData }) and as result get subscribed to all messages.

Is there a way to filter it (on server side) that it got subscribed to messages only in one chat room.

I saw that sockets had id's. And if the server side was using directly socket.io, I would just collect list of id's to which a messages needs to be sent.

However, Sails.js hides this level. And as result, I am not sure how to configure this on the server.

I saw similar question. However, it's reasonably old (taking into account speed of changes in Sails.js). I am not sure where should I put the code regarding Model.subscribe and Model.publish, taking into account that it's all hidden by convention (so it looks like I need to override some default behavior when it subscribe automatically).

Community
  • 1
  • 1
Victor Ronin
  • 22,758
  • 18
  • 92
  • 184
  • you should really read docs from post you've linked https://github.com/balderdashy/sails-docs/blob/0.9/sockets.md – monkeyinsight Aug 16 '14 at 06:46
  • @monkeyinsight: I did this. However, I did this on the website (sails.js), which apparently doesn't include this page. Also, this page didn't mention autoWatch (which is mentioned in reference guide). So, it was kind of hard to put it all togehter. – Victor Ronin Aug 16 '14 at 18:51

2 Answers2

2

First you need is override find and create blueprints according to this.

The second, take a closer look ad find blueprint. In order to achieve your goal you need update query criteria and join to proper (mean filtered) room.

Updating criteria example:

// Look up the model
var Model = actionUtil.parseModel(req),
    criteria = actionUtil.parseCriteria(req),
    user = req.user && req.user.id || 0,
    allowed = user ? [0, user] : [0],
    watchFilters = req.options.hasOwnProperty('watchFilters') ? req.options.watchFilters(req) : false,
    classRoom = "sails_model_create_" + Model.identity;

// i need filter by authenticated user so forbidden all other requests
if (allowed.indexOf(user) === -1) {
    return res.forbidden();
}

if (watchFilters) {
    classRoom = _.reduce(Object.keys(watchFilters), function (classRoom, field) {
        return classRoom + "_" + field + "_" + watchFilters[field];
    }, classRoom);
}

if (_.result(Model, 'ownerFiltered')) {
    criteria.user = user
}

There is single call of watch method little further,

if (req.options.autoWatch) { Model.watch(req); }

you need to replace it with this:

if (req.options.autoWatch) {
    sails.sockets.join(socket, classRoom);
    sails.log.silly("Subscribed socket ", sails.sockets.id(req.socket), "to", classRoom, "filtered by", watchFilters);
}

in order to join to proper room.

Third is replace call to publishCreate in create blueprint with own broadcast call:

Model.broadcast(classRoom, Model.identity, {
    verb: 'created',
    data: data,
    id: data[Model.primaryKey]
}, req.socket);

Now you able to filter your models with something like this:

watchFilters: function (req) {
    return {
        user: req.user.id,
        assembly: req.user.assembly || 1
    };
},
Community
  • 1
  • 1
1

First of all, I believe there are three pieces of documentation:

Frankly, it was hard to put it together only from documentation. So, I looked in the source code.

Anyway. The problem was that my model has an association with a collection. And as soon as my client read an object of my model, it became subscribed to the model of this collection. And I was seeing ALL changes for this collection model.

I found that there is a blueprint configuration called "Autowatch". It controls this behavior (whether to subscribe for a model of all associations). It works like a charm If you turn it off in config/blueprints.js.

Victor Ronin
  • 22,758
  • 18
  • 92
  • 184