Giter Site home page Giter Site logo

react_native_mqtt's Introduction

Build Status npm version

React Native Mqtt

This package is a wrapper around the javascript implementation of the paho mqtt client library to provide drop in compatibility with react native. If you happen to be running your own mqtt broker, it must support websockets for a connection to be possible.

Install

To install, use npm:

npm install react_native_mqtt --save

Usage

To use the library just pass in the options for the local storage module (react-native-storage) and the paho object will be attached to global scope.

import init from 'react_native_mqtt';
import { AsyncStorage } from 'react-native';

init({
  size: 10000,
  storageBackend: AsyncStorage,
  defaultExpires: 1000 * 3600 * 24,
  enableCache: true,
  reconnect: true,
  sync : {
  }
});

function onConnect() {
  console.log("onConnect");
}

function onConnectionLost(responseObject) {
  if (responseObject.errorCode !== 0) {
    console.log("onConnectionLost:"+responseObject.errorMessage);
  }
}

function onMessageArrived(message) {
  console.log("onMessageArrived:"+message.payloadString);
}

const client = new Paho.MQTT.Client('iot.eclipse.org', 443, 'uname');
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.connect({ onSuccess:onConnect, useSSL: true });

Example

To run the example, first make sure you have properly setup your env for react native development in ios and/or android: https://facebook.github.io/react-native/releases/0.21/docs/getting-started.html.

Then start the tsc compiler to watch and re-build changes, start up the js packager, and run android or ios.

cd /react-native-mqtt/
yarn
yarn run dev
cd /react-native-mqtt/example/
yarn
yarn start --reset-cache
cd /react-native-mqtt/example/
react-native run-android OR react-native run-ios

Lastly, once you have the example running, you can use this web app to verify that everything works as expected by publishing messages to the "WORLD" topic, using the config below.

Host: iot.eclipse.org
Port: 443
SSL: true

react_native_mqtt's People

Contributors

introvertuous avatar alikh31 avatar

Watchers

James Cloos avatar  avatar

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.