inProgress-team / react-native-meteor

Meteor Reactivity for your React Native application :)
MIT License
693 stars 210 forks source link

Meteor.subscribe vs Meteor.ddp.sub issue #225

Open license2e opened 7 years ago

license2e commented 7 years ago

Description: When subscribing directly using DDP (whether bundled with react-native-meteor using Meteor.ddp.sub or standalone libraries) the code works as expected:

  1. the connection is established
  2. the data is "added"
  3. the subscription is "ready"

However, when subscribing through Meteor.subscribe the following occurs:

  1. the connection is established
  2. the data is "added"
  3. the subscription is "ready"
  4. the data is "removed"
  5. the subscription is "nosub"
  6. the data is "added"
  7. the subscription is "ready"

Code

File: index.ios.js

import React from 'react';
import { AppRegistry, View } from 'react-native';
import Meteor from 'react-native-meteor';
// import App from './src/index';

Meteor.connect('wss://<domain>/websocket');

Meteor.waitDdpConnected(() => {
  // connect
  Meteor.ddp.on("connected", () => {
    console.log("Connected");
  });
  Meteor.ddp.on("disconnected", () => {
    console.log("Disconnected");
  });

  const userId = '<user-id>';
  // subscribe

  // =====================================
  // THIS WORKS FINE 
  // const subId = Meteor.ddp.sub("restricted", [userId]);

  // =====================================
  // THIS DOES NOT WORK AS EXPECTED
  const subId = Meteor.subscribe("restricted", userId, {
    onReady: () => {
      console.log('restricted ready');
    },
    onStop: () => {
      console.log('restricted stopped');
    },
  });

  console.log(subId);
  Meteor.ddp.on("ready", message => {
    console.log(message);
    if (message.subs.includes(subId)) {
      console.log("restricted ready");
    }
  });
  Meteor.ddp.on("added", message => {
    console.log(message);
  });
  Meteor.ddp.on("changed", message => {
    console.log(message);
  });
  Meteor.ddp.on("removed", message => {
    console.log(message);
  });
  Meteor.ddp.on("result", message => {
    console.log(message);
  });
  Meteor.ddp.on("nosub", message => {
    console.log(message);
  });
});

const App = () => <View />;

AppRegistry.registerComponent('RNApp', () => App);

Why is this happening?

Console log output when it works: (using Meteor.ddp.sub)

screen shot 2017-05-06 at 8 45 09 am

Console log output when it doesnt work as expected: (using Meteor.subscribe)

screen shot 2017-05-06 at 8 45 30 am
license2e commented 7 years ago

Could it possibly be related to this: https://github.com/inProgress-team/react-native-meteor/blob/master/src/Meteor.js#L277-L282 ?

Diwei-Chen commented 7 years ago

Thanks for posting this issue, @license2e

We are having this issue as well, FYI, @adamgins @JulianKingman,

which will lead to a splash on the screen within the subscription established.

screen shot 2017-07-17 at 2 19 26 pm

screen shot 2017-07-17 at 2 19 37 pm

Diwei-Chen commented 7 years ago

@license2e Wondering if you have any workaround for the Meteor.subscribe method that can restrict it to have the behaviours like Meteor.ddp.sub?

Thanks

adamgins commented 7 years ago

@license2e wondering if worked out solution to this?

Diwei-Chen commented 7 years ago

@license2e In our case, this only happens when we close the app and then reopen it.

As when we close the app, from my understanding, all the established connections will be lost in client side and we didn't cache Data.subscriptions, so each time when we open it, Data.subscriptions is just a plain object and this checking(https://github.com/inProgress-team/react-native-meteor/blob/master/src/Meteor.js#L206-L209) will completely get passed. So we always end up with creating a new subscription which may cause those unexpected behaviours(collections will be cleared up and re-initialised).

We are using @JulianKingman 's great react-native-meteor-offline library, so may try to add functionalities there to handle this case. @adamgins