My subscription stops automatically


#1

I use react (15.6.1) and react-router (v4) on client side. And it is the first time I am using react-router v4 (I used to use the previous versions). The problem is, when I subscribe to a publication on the client side, it immediately fires onStop callback with no error argument. I don’t stop subscription anywhere in my code, it stops itself. I’ve been trying to understand it for a whole day, yet I can’t solve the problem.

import React, { Component } from 'react'
import { createContainer } from 'meteor/react-meteor-data'

import { PagesCollection } from '/api/pages'

const defaultState = {
}

class RolesContainer extends Component {
  constructor(props) {
    super(props)

    this.state = defaultState
  }

  componentWillMount() {
    Meteor.subscribe('pages', {
      onReady: () => {
        console.log('ready')
      },
      onStop(err) {
        console.log('stop', err)
      }
    })
  }

  render() {
    const { pages } = this.props

    return(
      <Roles context={this}/>
    );
  }
}

export default createContainer(() => {
  return {
    pages: PagesCollection.find().fetch(),
  }
}, RolesContainer)

I subscribe to publication inside componentWillMount (as I always did in my previous projects).

What could be the problem?


#2

From what I can tell from this code there isn’t a reason for the subscription stopping like this. The only thing I could possibly think of that would cause this is publication code throwing an error.

On a side note, if you make your subscribe calls from within createContainer it will handle the cleanup when the component is unmounted which is a pretty nice feature since you don’t need to manually stop subscriptions.


#3

This is happening to me as well – I’m using Meteor.subscribe to subscribe to a publication in a part of my React app, but it’s instantly stopped.

I traced the reason for it, and from what I can tell, it’s stopped by Meteor. I traced other similar client subscriptions in a pure Meteor app, and they were all automatically resubscribed to by Meteor; probably due to some reactive black magic.

I think the idea is to but subscriptions inside the createContainer function (now withTracker) and feed the wrapped component with a collection.find().fetch() call. But sadly that doesn’t work for me either, since the whole React app is invalidated and re-rendered whenever my subscribed collection is updated, haha…


#4

Subscriptions must be inside withTracker. Then use shouldComponentUpdate to control renders if you need to