GithubHelp home page GithubHelp logo

corymsmith / react_native_mqtt Goto Github PK

View Code? Open in Web Editor NEW

This project forked from introvertuous/react-native-mqtt

0.0 2.0 0.0 31 KB

๐ŸŽ Mqtt client for react native.

License: MIT License

JavaScript 100.00%

react_native_mqtt's Introduction

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. Mosquitto does not support websockets out of the box and will require some extra work. Another broker that does have support for websockets is aedes, which is the broker I use personally.

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,
  sync : {
  }
});

function onConnect() {
  console.log("onConnect");
  var message = new Paho.MQTT.Message("Hello");
  message.destinationName = "/World";
  client.send(message);
}

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

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

var client = new Paho.MQTT.Client('test.mosquitto.org', 8080, 'unique_client_name');
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.connect({onSuccess:onConnect});

This is how you can use the aedes broker to handle websocket and standard connections.

var net = require('net');
var aedes = require('aedes');
var websocket = require('websocket-stream');

exports.listen = function() {
  aedes = aedes();
  var server = net.createServer(aedes.handle);
  server.listen(1883);
  websocket.createServer({port: 8080}, aedes.handle);
}

react_native_mqtt's People

Contributors

introvertuous avatar

Watchers

 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.