Coder Social home page Coder Social logo

react-native-meteor's Introduction

@socialize/react-native-meteor

react-native-meteor npm version Dependency Status Gitter Chat

This project was adapted from react-native-meteor by inProgress Team to be more up to date and focused. This documentation has been revised to be more coherent, dependencies have been updated, and the API has been brought more in-line with Meteor.

If you are moving from that package to this one, you may find certain parts are missing that were deemed to be outside the scope of this package (ListViews), were removed/deprecated in meteor core or are outdated methods of connecting Tracker with your componenets (connectMeteor, composeWithTracker and createContainer).

Supporting The Project

In the spirit of keeping this and all of the packages in the Socialize set alive, I ask that if you find this package useful, please donate to it's development.

Litecoin

Patreon / Paypal

Installation And Setup

$ npm i --save @socialize/react-native-meteor

Android

Add the following permission to your AndroidManifest.xml file for faster reconnects to the DDP server when your device reconnects to the network.

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

If running an android emulator you have to forward the port of your meteor app.

$ adb reverse tcp:3000 tcp:3000

Example Usage

import React, { Component } from 'react';
import { View, Text } from 'react-native';
import Meteor, { withTracker } from 'react-native-meteor';
import TodosCollection from '../api/todo';

Meteor.connect('ws://192.168.X.X:3000/websocket');//do this only once

class App extends Component {
  renderRow(todo) {
    return (
      <Text>{todo.title}</Text>
    );
  }
  render() {
    const { todos, todosReady } = this.props;

    return(
      <View>
          {todosReady ? todos.map(renderRow) : <View>Loading...</View>}
      </View>
    )
  }
}

export default withTracker(params=>{
  return {
    todosReady: Meteor.subscribe('todos').ready(),
    todos: TodosCollection.find({done: false}, { sort: { createdAt: -1 }}),
  };
})(App);

Reactive Variables

These variables can be used inside withTracker. They will be populated into your component if they change.


API

Subscriptions

import Meteor, { Tracker } from '@socialize/react-native-meteor';

const handle = Meteor.subscribe('socialize.friends');

Tracker.autorun(() =>{
    if(handle.ready()){
        console.log('Subscription ready...');
        handle.stop();
    }
});

Collections

These methods work offline. That means that elements are correctly updated offline, and when you reconnect to ddp, Meteor calls are taken care of.

You need pass the cursoredFind option when you get your collection if you want to use cursor-like method:

import { Mongo } from '@socialize/react-native-meteor';

Mongo.Collection("collectionName", { cursoredFind: true });

Or you can simply use find() to get an array of documents. The option default to false for backward compatibility. Cursor methods are available to share code more easily between a react-native app and a standard Meteor app.

Meteor.is/Environment/

Keeping in line with Meteor's API, isClient is provided as well as a isReactNative, similar to how Meteor provides isCordova when code is running in a cordova build. isServer and isCordova are not provided as they will still be falsey when checking. These properties allow for code reuse across your codebases.

  • Meteor.isClient - True
  • Meteor.isReactNative - True

DDP connection

Meteor.connect(url, options)

Connect to a DDP server. You only have to do this once in your app.

Arguments

  • url string required
  • options object Available options are :
    • autoConnect boolean [true] whether to establish the connection to the server upon instantiation. When false, one can manually establish the connection with the Meteor.ddp.connect method.
    • autoReconnect boolean [true] whether to try to reconnect to the server when the socket connection closes, unless the closing was initiated by a call to the disconnect method.
    • reconnectInterval number [10000] the interval in ms between reconnection attempts.

Meteor.ddp

Once connected to the ddp server, you can access every method available in ddp.js.

  • Meteor.ddp.on('connected')
  • Meteor.ddp.on('added')
  • Meteor.ddp.on('changed')

Meteor.disconnect()

Disconnect from the DDP server.

Meteor Methods

Additional Packages

ReactiveDict

import { reactivedict } from 'react-native-meteor';

See documentation.

Accounts

import Meteor, { Accounts } from 'react-native-meteor';

React Meteor Data

import { withTracker } from 'react-native-meteor';

See documentation.


Contribution

Pull Requests and issues reported are welcome! :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.