Git Product home page Git Product logo

Comments (7)

marshallswain avatar marshallswain commented on August 22, 2024

This doesn't take care of the custom events issue, but is it currently possible to provide an array of event filters the same way it's done for hooks? If not, that would be a really great thing to include, imo. Then we can have a filter library the same way that we will for hooks.

from socketio.

daffl avatar daffl commented on August 22, 2024

Not at the moment but feathersjs-ecosystem/commons#15 will add all of that. As you suggested, event filtering works pretty much the same as hooks, except that you will - additionally to the hook object - also get the current dispatch data and the connection (params in the old hooks). So you can do things like:

const todos = app.service('todos');

// Blanket filter out all connections that don't belong to the same company
todos.filter(function(data, connection) {
  if(data.company_id !== connection.user.company_id) {
    return false;
  }

  return data;
});

// After that, filter todos, if the user that created it
// and the connected user aren't friends
todos.filter('created', function(data, connection, hook) {
  const todoUserId = hook.params.user._id;
  const currentUserFriends = connection.user.friends;

  if(currentUserFriends.indexOf(todoUserId) === -1) {
    return false;
  }

  return data;
});

// Other ways to use it
todos.filter({
  removed: [a, b],
  updated: function(data, connection, hook, callback) {}
});

All of that will be backwards compatible with the legacy created, updated etc. methods.
Would you mind pasting the above snippet into the docs so that we don't forget about?

from socketio.

marshallswain avatar marshallswain commented on August 22, 2024

Nope, I don't mind. I added it to the events page.

from socketio.

ekryski avatar ekryski commented on August 22, 2024

Now that this landed in feathers-commons it should be good to close.

from socketio.

daffl avatar daffl commented on August 22, 2024

I still have to make a feathers-socket-commons and feathers-socketio and feathers-primus release. Just wrapping up a couple of tests but should be prety quick.

from socketio.

green-coder avatar green-coder commented on August 22, 2024

At https://docs.feathersjs.com/api/events.html#event-filtering it is written :

Filter functions run for every connected client on every event and should be optimized for speed and chained by granularity.

I am wondering if for some cases, instead of running a filter for each connection, that would not be more efficient to reverse the relation and just group the connections by criteria (e.g. by room), run no filters and send the grouped connections the event?

Would this way of filtering be allowed by / pluggable into the current implementation of feathersjs-socketio?

from socketio.

green-coder avatar green-coder commented on August 22, 2024

Nevermind, I found the related issue.
feathersjs/feathers#388 (comment)

from socketio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.