Git Product home page Git Product logo

ionic-mqtt's Introduction

ionic-mqtt

**UPDATES: This package is being migrated to ionic v4.

Introduction

This is a simple ionic wrapper of paho-mqtt. For more detail of paho-mqtt, please refer to this paho-mqtt github page.

Get Started

Installation

npm install ionic-mqtt --save

Example Usage


// inside your app.module.ts

...
import { IonicMqttModule, MQTTService } from 'ionic-mqtt';

@NgModule({
  ...,
  imports: [
    ...,
    IonicMqttModule
  ],
  providers: [
    ...,
    MQTTService
  ],
});

export class AppModule {}


// inside your component

import { MQTTService } from 'ionic-mqtt';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})

export class HomePage {

  private _mqttClient: any;

  private MQTT_CONFIG: {
    host: string,
    port: number,
    clientId: string,
  } = {
    host: "test.mosquitto.org",
    port: 8081,
    clientId: "mqtt",
  };

  private TOPIC: string[] = [];

  constructor(private mqttService: MQTTService) {
  }

  ngOnInit() {
    this._mqttClient = this.mqttService.loadingMqtt(this._onConnectionLost, this._onMessageArrived, this.TOPIC, this.MQTT_CONFIG);
  }

  private _onConnectionLost(responseObject) {
    // connection listener
    // ...do actions when connection lost
    console.log('_onConnectionLost', responseObject);
  }

  private _onMessageArrived(message) {
    // message listener
    // ...do actions with arriving message
    console.log('message', message);
  }

  ...


  // public function for sending and publishing mqtt messages

  public sendMessage() {
    console.log('sendMessage')
    this._mqttService.sendMessage(TOPIC, MESSAGE);
  }

  public publishMessage() {
    console.log('publishMessage')
    this._mqttService.publishMessage(TOPIC, MESSAGE);
  }

  ...

}

// inside your component html

<ion-content padding>

  ...

  <button (click)="sendMessage()">Send Message</button>
  <button (click)="publishMessage()">Publish Message</button>

  ...

</ion-content>

The above example uses the Mosquitto broder/server, if you want to run your own localhost MQTT broker, you can use Mosquitto or Mosca, to launch your own broker. Remember to change the host, port and clientId to your localhost server respectively.

API

MQTTService.loadingMqtt(connection_callback, message_callback, topic(s), configurations)

* connection_callback

callback function (response) {}

called when a connection has been lost. after a connect() method has succeeded. Establish the call back used when a connection has been lost. A single response object parameter is passed to the onConnectionLost callback containing the following fields:

  1. errorCode
  2. errorMessage

* message_callback

callback function (message) {}

called when a message has arrived. Parameters passed to the onMessageArrived callback are:

  1. Paho.MQTT.Message

* topic(s)

string[]

mandatory the array of the topics in string to which the message is to be published.

* configurations

host: string

the address of the messaging server, as a fully qualified WebSocket URI, as a DNS name or dotted decimal IP address.

port: number

the port number to connect to - only required if host is not a URI.

path?: string

the path on the host to connect to - only used if host is not a URI. Default: '/mqtt'.

clientId: string

the Messaging client identifier, between 1 and 23 characters in length.

MQTTService.publishMessage(topic, playload, qos, retained)

Publish a message to the consumers of the destination in the Message. Synonym for Paho.Mqtt.Client#send

topic: string

mandatory the name of the topic to which the message is to be published.

playload: string

The message data to be published.

qos?: number

The Quality of Service used to deliver the message. 0 Best effort (default).

  1. At least once.
  2. Exactly once.

retained?: boolean

If true, the message is to be retained by the server and delivered to both current and future subscriptions. If false the server only delivers the message to current subscribers, this is the default for new Messages.

Usage with Ionic

Pre-requisites

Before you can begin using these mqtt service with your project, you need to make sure your project meets a few of these requirements:

"typescript" >= 3.4.3

"@ionic-angular" >= 4.7.1

"@angular/core" >= 8.1.2

Contributing

This ionic-mqtt wrapper is an Open Source Package. So it is open to any contributors.

License

MIT

ionic-mqtt's People

Contributors

vinidg avatar ketherchau avatar 0x-kchau avatar

Watchers

James Cloos 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.