edusperoni / nativescript-mqtt

MQTT 3.1.1 for Nativescript
Apache License 2.0
12 stars 3 forks source link

doesn't work with last natvescript #13

Closed peppeg85 closed 4 years ago

peppeg85 commented 4 years ago

hello is there a way to make it works with the last NS (I use pure JS)?

here is my code(very basic)

    import {MQTTClient} from "nativescript-mqtt";
    const Observable = require("tns-core-modules/data/observable").Observable;

    var pageData = new Observable({
         message: '',
    })

exports.loaded = function (args) {
    var pageContent = args.object;
    pageContent.bindingContext = pageData;
    pageData.message='hello'
};

let mqtt_host  = "test.mosquitto.org";
let mqtt_port  = 1883;
let mqtt_useSSL  = false;
let mqtt_cleanSession  = false;

let mqtt_clientOptions = {
    host: mqtt_host,
    port: mqtt_port,
    useSSL: mqtt_useSSL,
    cleanSession: mqtt_cleanSession
};

let mqtt_client = new MQTTClient(mqtt_clientOptions);
let mqtt_topic = "topic/room";

exports.connect = function(){
    console.log('connect')

    const opts = {
        qos: 1
    };

    mqtt_client.onConnectionFailure.on((err) => {
        console.log("Connection failed: " + JSON.stringify(err));
    });
    mqtt_client.onConnectionSuccess.on(() => {
        console.log("Connected successfully!");
        alert("Connected successfully!");
        mqtt_client.subscribe(mqtt_topic, opts);
    });
    mqtt_client.onConnectionLost.on((err) => {
        console.log("Connection lost: " + JSON.stringify(err));
    });
    mqtt_client.onMessageArrived.on((message) => {
        console.log("Message received: " + message.payload);
    });
    mqtt_client.onMessageDelivered.on((message) => {
        console.log("Message delivered: " + message.payload);
    });
    mqtt_client.connect("", "");

    };
DarrellBrogdon commented 4 years ago

There appears to be a pretty severe bug in the Websockets plugin that this plugin relies on and, unfortunately, that project's maintainer indicated last year that he wasn't in a position to fix it.

peppeg85 commented 4 years ago

sorry, but is there any other plugin to use mqtt on NS?

DarrellBrogdon commented 4 years ago

Not that we've been able to find. We chose to abandon MQTT in our app and switched to a polling mechanism. MQTT would've been ideal but we were under pressure to deliver on a timeline. For a future version we've entertained the idea of building native MQTT components for both Android and iOS and simply exposing those to our app.

peppeg85 commented 4 years ago

Ok, so i Think i will use firebase with real time database, or Rest api, i'm using a esp32 and want to make an app to show the acquired data

edusperoni commented 4 years ago

@peppeg85 this project focuses on MQTT over websockets, so you can't use a pure MQTT port. I believe test.mosquitto.org accepts 8080 as HTTP and 8081 as SSL, so you have to use those ports.

This follows the same structure as the paho-mqtt library for the web.

About the nativescript-websockets lib. The only issue with that is it occasionally breaks the connection after a few minutes on iOS, if you turn on reconnection, it shouldn't be a big issue.