Skip to content

Commit

Permalink
Auto reconnects fastly to DDP server if you device reconnects to network
Browse files Browse the repository at this point in the history
  • Loading branch information
Théo mathieu committed Mar 8, 2016
1 parent 6520158 commit 15d9ea6
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

# react-native-meteor

Meteor-like methods for React Native. **Currently in v1.0.0-beta6** ! For old docs, see [v0.6.2 documentation](https://github.com/inProgress-team/react-native-meteor/tree/0.6.2) (classic ddp interface).
Meteor-like methods for React Native. **Currently in v1.0.0-beta7** ! For old docs, see [v0.6.2 documentation](https://github.com/inProgress-team/react-native-meteor/tree/0.6.2) (classic ddp interface).

## What is it for ?

Expand Down
10 changes: 10 additions & 0 deletions docs/Install.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# Android

Add this to your AndroidManifest.xml file to autoreconnect fastly to DDP server if your device reconnects to network

```xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
```



# Installing decorators

## With RN >= 0.20.0 (Babel 6)
Expand Down
1 change: 1 addition & 0 deletions example/mobile/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package="com.mobile">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:allowBackup="true"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-meteor",
"version": "1.0.0-beta6",
"version": "1.0.0-beta7",
"description": "DDP React-native Client",
"main": "src/Meteor.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions src/Data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import minimongo from 'minimongo-cache';
process.nextTick = setImmediate;

export default {
endpoint: null,
options: null,
ddp: null,
subscriptions: {},
db: new minimongo(),
Expand Down
23 changes: 23 additions & 0 deletions src/Meteor.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NetInfo } from 'react-native';

import reactMixin from 'react-mixin';
import Trackr from 'trackr';
import DDP from '../lib/ddp.js';
Expand Down Expand Up @@ -54,7 +56,17 @@ module.exports = {
Data.ddp.disconnect();
}
},
_reconnect() {
if(Data._endpoint) {
this.connect(Data._endpoint, Data._options);
}
},
connect(endpoint, options) {
Data._endpoint = endpoint;
Data._options = options;



this.ddp = Data.ddp = new DDP({
endpoint: endpoint,
SocketConstructor: WebSocket,
Expand All @@ -64,6 +76,16 @@ module.exports = {
Data.ddp.on("connected", ()=>{
console.info("Connected to DDP server.");
this._loadInitialUser();


if(!this._netInfoListener) {
this._netInfoListener = isConnected=>{
if(isConnected) {
this._reconnect();
}
};
NetInfo.isConnected.addEventListener('change', this._netInfoListener);
}
});

Data.ddp.on("disconnected", ()=>{
Expand Down Expand Up @@ -103,6 +125,7 @@ module.exports = {
}
}
});

},
subscribe(name) {
var params = Array.prototype.slice.call(arguments, 1);
Expand Down

2 comments on commit 15d9ea6

@tvogels
Copy link

@tvogels tvogels commented on 15d9ea6 Mar 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should you also remove the _netInfoListener on disconnect?

@Mokto
Copy link
Contributor

@Mokto Mokto commented on 15d9ea6 Mar 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so.

Cause every time the network state is online, you have to try reconnecting to the server.
When you are offline, disconnected event is triggered and you have to be ready to listen to online state.

Please sign in to comment.