GitXplorerGitXplorer
K

mqtt-react

public
47 stars
31 forks
23 issues

Commits

List of commits on branch master.
Verified
9c2639b57041a8bb2d0e41598fb4cb340bbc4554

Update README.md

KKeKs0r committed 6 years ago
Unverified
d831e8257812d37d303560c0f5ef126a69a8f866

Merge pull request #3 from KeKs0r/handler_on_construct

KKeKs0r committed 8 years ago
Unverified
39e22f09762e56ca22b2cc7045e4358c9076257f

use instance handler

KKeKs0r committed 8 years ago
Unverified
92b4be8fec2d05278107c40783a4d670df8a5cbd

message handler in constructor

KKeKs0r committed 8 years ago
Unverified
16797262eefe5f0685bba0c779435f0a51918742

readme: Demo

KKeKs0r committed 8 years ago
Unverified
9c442df5c00e9944ef57c7a50bfbcd3a3b0e9433

0.0.2

KKeKs0r committed 8 years ago

README

The README file for this repository.

Build Status

Maintenance

I am not using this library, so it is really hard to maintain it, without knowing the use cases. If someone is interested in taking it over or supporting the maintenance. Please reach out to me. My email is on my profile.

mqtt-react

React Container for mqttjs/MQTT.js

Demo

There is a very minimalistic Demo-App: mqtt-react-demo

Usage

Currently, mqtt-react exports two enhancers. Similarly to react-redux, you'll have to first wrap a root component with a Connector which will initialize the mqtt instance and then subscribe to data by using subscribe.

Root component

The only property for the connector is the connection information for mqtt.Client#connect

Example Root component:

import { Connector } from 'mqtt-react';
import App from './components/App';

export default () => (
  <Connector mqttProps="ws://test.mosca.io/">
    <App />
  </Connector>
);

Subscribe

Example Subscribed component:

import { subscribe } from 'mqtt-react';

// Messages are passed on the "data" prop
const MessageList = (props) => (
  <ul>
    {props.data.map( message => <li>{message}</li> )}
  </ul>
);

// simple subscription to messages on the "@test/demo" topic
export default subscribe({
  topic: '@demo/test'
})(MessageList)

Example Posting Messages

MQTT Client is passed on to subscribed component and can be used to publish messages via mqtt.Client#publish

import React from 'react';
import { subscribe } from 'mqtt-react';

export class PostMessage extends React.Component {
    
  sendMessage(e) {
      e.preventDefault();
      //MQTT client is passed on
      const { mqtt } = this.props;
      mqtt.publish('@demo/test', 'My Message');
  }  
  
  render() {
    return (
      <button onClick={this.sendMessage.bind(this)}>
        Send Message
      </button>
    );
  }
}

export default subscribe({
  topic: '@demo/test'
})(PostMessage)

Advanced Susbcription / Integration with Redux:

It is possible to provide a function that handles received messages. By default the function adds the message to the data prop, but it can be used to dispatch actions to a redux store.

import { subscribe } from 'mqtt-react';
import store from './store';


const customDispatch = function(topic, message, packet) {
    store.dispatch(topic, message);
}


export default subscribe({
  topic: '@demo/test',
  dispatch: customDispatch
})

Credits

Sponsored by nearForm

Contributing

Pull Requests are very welcome!

If you find any issues, please report them via Github Issues!

Contributors

License

(MIT)