Skip to content
This repository was archived by the owner on Aug 29, 2018. It is now read-only.

Filter service updates by ID #87

Closed
nvitas opened this issue Sep 19, 2017 · 1 comment
Closed

Filter service updates by ID #87

nvitas opened this issue Sep 19, 2017 · 1 comment

Comments

@nvitas
Copy link

nvitas commented Sep 19, 2017

I'm trying to listen for updates on an 'event' service. I'm trying to listen to just the ones with a specific id.

This will give me all updates:

socket.on('event updated', (rEvent) => {
      console.log('Got an updated Event!', rEvent);
});

But what I'm looking for is something like this (only event w/id 34):

socket.on('event/34 updated', (rEvent) => {
     console.log('Got an updated Event!', rEvent);
});

I've gone through the docs, filtering, chat app...I don't have 'users' so I can't join an 'event' service on the user id. I'm struggling w/how to create a room for that specific 'event'.

What's the best approach for this?

@daffl
Copy link
Member

daffl commented Sep 21, 2017

Events are cheap and - as long as the client is allowed to see them which is what event filters determine - it is much easier for the client to just ignore it if they don't need it. We recommend not to keep track of the user state on the server because it is pretty bad for performance but to answer your question, you can create a custom event to subscribe to specific ids:

socket.on('subscribe', (service, ids) => {
  socket.feathers.eventSubscriptions = {
    [service]: ids
  };
});

Then in a filter on the server:

app.service('event').filter('updated', (data, connection) => {
  if(connection.eventSubscriptions.ids.indexOf(data._id) === -1) {
    return false;
  }

  returnd data;
});

@daffl daffl closed this as completed Oct 16, 2017
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants