eddyystop/feathers-reduxify-services

Synchronize feathers events with redux state tree.

Closed this issue · 3 comments

Feathers provides us with the on method, which allows us to listen to feathers events. Also, the callback gives us the changed data, and that lets us know which part are changed.

However, the redux state tree and feathers' events are not automatically synchronized. For instance, if I patched something in the user service, feathers events will emit the change to our feathers client, but the redux state tree won't know that it needs to be updated, so they wouldn't be in sync.

So, if feathers-reduxify-services can provide us with an action to update the state tree automatically, that would be great. I know that it's possible to use the .on action of feathers-reduxify-services, but an action that simplifies the process would be nice to have.

Here's a simple example of how this would be implemented. There should be an additional action to explicitly reduce the state tree based on the incoming data, and it could be named sync.

app.service("users").on("patched", data => {
  dispatch(services.users.sync("patched", data))
})

This should preferably be entirely optional, because not everyone will want this feature, especially the compatibility issues it might bring, and some may want to deal with state updates by themselves.

Thanks for the input. I have been interested in this issue for some time. Unfortunately the implementation right now would get rather complicated.

My feeling is this is less a CRUD issue than one of the client maintaining a real-time local copy of part of a database table, like Meteor does.

Once Feathers' Auk version is soon released, a priority for the core-team is to implement a chat-rooms like feature for Feathers filters. (The lack of such a feature is what makes this issue complicated at present.)

I feel this can be used to create a publisher/subscriber mechanism between part of the database table and the client.

Any change to the table, along with a 'data update happened' flag, would be reflected in the Redux state.

So this'll get done but the timing is unknown.

Related to #7

Replication engines generally maintain a near realtime, local copy of (some of) the records in a service on the server. feathers-reduxify-services now provides an interface which you can use to interface replication engines with the Redux state for the service.

This interface updates the new state property store which has been added to the previous ones: isError, isLoading, isSaving, isFinihsed, data and queryResult.

feathers-offline-realtime is the official Feathersjs realtime replication engine. Please read its README.

It can be interfaced with feathers-reduxify-services as follows:

import reduxifyServices from 'feathers-reduxify-services';
import Realtime from 'feathers-offline-realtime';

const services = reduxifyServices(app, ['messages', ...]);
const store = applyMiddleware( ... , messages: services.messages.reducer }));

const messagesRealtime = new Realtime(feathersApp.service('/messages'));

messagesRealtime.on('events', (records, last) => {
  store.dispatch(services.messages.store({ connected: messagesRealtime.connected, last, records }));
});

This would create the state:

state.messages.store = {
  connected: boolean, // if replication engine still listening to server
  last: { activity, eventName, record }, // last activity. See feathers-offline-realtime
  records: [ objects ], // the near realtime contents of the remote service
};

In the next few days this repo will be brought into Feathersjs itself as feathers-redux and the repo itself will be deprecated.