Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscribe to the same service with different queries. #134

Closed
ruimgoncalves opened this issue Apr 26, 2015 · 5 comments
Closed

Subscribe to the same service with different queries. #134

ruimgoncalves opened this issue Apr 26, 2015 · 5 comments

Comments

@ruimgoncalves
Copy link

Hi

Is it possible to implement a service that keeps its client synced based on its query?

For example, Client A connects to todos collection and asks for completed collections list, Client B connects to the same todos collections but asks for the uncompleted list.
When client B changes one uncompleted item to completed, it should be removed from clients B collection and added to client A.
This should be valid for any query supported by the service.

Is this simple to implement?
Can you give me some pointers to do this?

Thanks.

@ruimgoncalves
Copy link
Author

I managed to get this to work for a simple memory store with simple queries, but should be possible to implement with other stores.
The changes are propagated to the right client based on his query, and items are added and removed correctly.
This is functioning on 2 separated clients, but now I want to be able to do this on the same client, without opening multiple io sockets per query. I was thinking about "chat rooms" but have no idea on how to implement and track "chat rooms" in the service.
Can anyone give me some pointers?
Thanks

@daffl
Copy link
Member

daffl commented May 1, 2015

I think that depends on your implementation. How does it look like at the moment? I see a couple of different ways to make that happen. You could track which parameters the client is listening for and then create some client side logic that keeps the proper list in sync. Or add your own socket events for each parameter combination. You could also add SocketIO rooms although it wouldn't happen in the service but the SocketIO configuration instead. Maybe something like:

app.configure(feathers.socketio(function(io) {
  var query = { complete: true };
  // Create a room for that query (room name is the JSON of `query`)
  var room = io.of(JSON.stringify(query));
  var todoService = app.service('todos');
  // A handler when a Todo has been changed
  var todoChanged = function(todo) {
    // now find all Todos
    todoService.find({ query: query }, function(error, todos) {
      // emit a `todos.find` event with the new data
      room.emit('todos.find', todos);
    });
  };

  // Listen to all changed
  todoService.on('updated', todoChanged);
  todoService.on('patched', todoChanged);
  todoService.on('created', todoChanged);
  todoService.on('removed', todoChanged);
}));

@ruimgoncalves
Copy link
Author

Hi Daff

Thanks for reply

Here is my WIP of the LiveQuery service adapter, please bare in mind that this is a ongoing WIP and did not clean the code thus having some comments and console.log in Portuguese.

I suppose there is a better way to do this, but my intent was to make an adapter to easily implement any store. For now I'm using memory-store but should be easily ported to support mongo.

Now the idea is to add pagination support, and support multiple livequeries per client.

I'm using nodemon with coffescript support to run this package.
Open 2 browser windows with this links
http://localhost:3000/?complete=true
http://localhost:3000/?complete=false

Please be free to use the code in any way you want, and I'll appreciate if you can give me some pointers.

Thanks

@ekryski
Copy link
Contributor

ekryski commented Dec 28, 2015

You should be able to simply use feathers-client and listen for update events. Then I would just filter on a per client basis. Like @daffl said you could also use an after hook using feathers-hooks to fire a custom event into a socket.io room for specific queries.

Those are just a couple ideas off the top of my head. Many ways to skin a cat. Going to close this for now as I don't see us implementing automatic event triggers based on query params. That would start to get crazy.

@lock
Copy link

lock bot commented Feb 8, 2019

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue with a link to this issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Feb 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants