Subscription in redux action unsubscribes immediately


#1

I stumbled on a weird issue. I’m subscribing to a publication, the publication just returns a minimongo cursor. Subscribing to this publication works without issues when used inside a trackerToProps function or when used anywere else except in a redux action…

Here’s my code:

export const fetchStuff = guid => (dispatch) => {

  dispatch({
    type: STUFF_FETCH,
    data: { guid }
  });


    Meteor.subscribe('stuff', { guid }, {
      onReady() {
        const doc = Collection.findOne({ guid });
        dispatch({
          type: SUBSCRIBED_TO_STUFF,
          data: { doc }
        });
      }
    }); 
};

Does anyone have an idea on why this happens? Why does my subscription subscibe, because ‘ready’ and has the data, then immediately unsubscribes and of course removes the data? Of course I can work around this, but I want to understand it :slight_smile:


#2

So here’s a small follow up. It seems that in redux actions (likely because I’m using react-redux-meteor-data) it automatically kills subscriptions when they have finished. It seems logical, because data resulting from this action should end up in the state when reducers pick up the dispatched event “SUBSCRIBED_TO_STUFF”.

However, this is inconvenient for me since I prefer minimongo over putting my domain state in redux.

How I solved it now is to simply put my subscription in a function giving it any params including the dispatch function as parameters. Then I call that function from within my action. This means that the subscription is not automatically stopped whenever the action has finished.

Is there a ‘cleaner’ way of handling this?

Here’s my solution:

function subscribeToStuff(dispatch, guid) {
    Meteor.subscribe('stuff', { guid }, {
      onReady() {
        const doc = Collection.findOne({ guid });
        dispatch({
          type: SUBSCRIBED_TO_STUFF,
          data: { doc }
        });
      }
    }); 
}

export const fetchStuff = guid => (dispatch) => {

  dispatch({
    type: STUFF_FETCH,
    data: { guid }
  });

  subscribeToStuff(dispatch, guid);
};

#3

I also have this problem but in my case when I apply @cloudspider’s solution onReady is never called, only onStop.

Are you still using this or were you able to find a cleaner solution @cloudspider?

Edit: I’m now using https://github.com/samybob1/meteor-redux-middlewares which seems to to do job.


#4

My subscription stop seemed to have been caused by a transform function in the publication on the serverside. There was no error, but it did unsubscribe directly. Also instead of the onStop i’m using the onError callback.

I’ve never had any issues since then and when I would have, the publication is the first place I would troubleshoot.