edusperoni / nativescript-mqtt Goto Github PK
View Code? Open in Web Editor NEWMQTT 3.1.1 for Nativescript
License: Apache License 2.0
MQTT 3.1.1 for Nativescript
License: Apache License 2.0
iOS, emulator & physical device.
Works on Android.
tns --version
to fetch it)7.0.8
Cross-platform modules: (check the 'version' attribute in the
node_modules/tns-core-modules/package.json
file in your project)
"@nativescript/core": "~7.0.10",
"@nativescript/ios": "7.0.1"
The problem actually occurs in the underlying paho-mqtt.js file. Line 733. It looks like maybe NS doesn't export clearTimeout as a global any more but that's purely a guess. I tried a couple of import/require statements in the file but couldn't get it to work.
It's possible this is a bug in NS itself in the switch to V8. I can file a bug there if that is indeed the case
TypeError: clearTimeout is not a function
at Pinger.reset (file:///app/vendor.js:98371:5)
at ClientImpl.LibraryFactory.ClientImpl._socket_send (file:///app/vendor.js:99115:20)
at ClientImpl.LibraryFactory.ClientImpl._on_socket_open (file:///app/vendor.js:98854:9)
at NativeWebSockets.onopen (file:///app/vendor.js:97821:14)
at NativeWebSockets.push.NativeWebSockets._notifyBrowser (file:///app/vendor.js:64339:29)
at NSObject_2.webSocketDidOpen
tns --version
to fetch it)7.0.10
"dependencies": {
"@nativescript-community/ui-pager": "^13.0.0-alpha.10",
"@nativescript/appversion": "^2.0.0",
"@nativescript/core": "~7.0.10",
"@nativescript/firebase": "^11.0.0",
"@nativescript/geolocation": "^7.0.0",
"@nativescript/iqkeyboardmanager": "^2.0.0",
"@nativescript/secure-storage": "^3.0.0",
"@nativescript/theme": "^3.0.0",
"@types/node": "^12.12.7",
"moment": "^2.27.0",
"moment-timezone": "^0.5.28",
"nativescript-audio-player": "^1.0.3",
"nativescript-insomnia": "^2.0.0",
"nativescript-mqtt": "^3.0.0",
"nativescript-ui-listview": "^9.0.2",
"nativescript-ui-sidedrawer": "^9.0.3"
},
"devDependencies": {
"@nativescript/android": "7.0.0",
"@nativescript/ios": "7.0.1",
"@nativescript/types": "7.0.0",
"@nativescript/webpack": "~3.0.0",
"node-sass": "^4.14.1",
"tslint": "^6.1.1",
"typescript": "3.9.7"
},
I'm getting this error from onConnectionFailure:
{
"errorCode": 16,
"errorMessage": "AMQJS0016E Invalid MQTT message type 0."
}
Google suggests react native once had a similar issue caused by converting to utf8 instead of base64. I can't find much in the way of what the error actually means.
tns --version
to fetch it)"dependencies": {
"@nativescript-community/ui-pager": "^13.0.11",
"@nativescript/appversion": "^2.0.0",
"@nativescript/core": "~7.1.0",
"@nativescript/firebase": "^11.0.0",
"@nativescript/geolocation": "^7.0.0",
"@nativescript/iqkeyboardmanager": "^2.0.0",
"@nativescript/secure-storage": "^3.0.0",
"@nativescript/theme": "^3.0.0",
"@nstudio/nativescript-exoplayer": "^5.0.4",
"@proplugins/nativescript-masked-text-field": "^6.0.0",
"@proplugins/nativescript-orientation": "^2.5.0",
"@types/node": "^12.12.7",
"moment": "^2.27.0",
"moment-timezone": "^0.5.28",
"nativescript-audio-player": "^1.0.3",
"nativescript-google-maps-sdk": "^3.0.2",
"nativescript-insomnia": "^2.0.0",
"nativescript-mqtt": "^3.0.1",
"nativescript-ui-listview": "^9.0.2",
"nativescript-ui-sidedrawer": "^9.0.3",
"nativescript-webview-interface": "^1.4.4",
"nativescript-websockets": "^1.5.6"
},
"devDependencies": {
"@nativescript/android": "7.0.1",
"@nativescript/ios": "7.2.0",
"@nativescript/types": "7.0.0",
"@nativescript/webpack": "~4.0.0",
"node-sass": "^4.14.1",
"tslint": "^6.1.1",
"typescript": "3.9.7"
},
Describe the steps to reproduce it.
upon calling mqtt.subscribe I get:
====== Assertion failed ======
Native stack trace:
1 0x104cbc9ff tns::Assert(bool, v8::Isolate*) + 119
2 0x104c3fc39 tns::ArgConverter::Invoke(v8::Localv8::Context, objc_class*, v8::Localv8::Object, tns::V8Args&, tns::MethodMeta const*, bool) + 95
3 0x104c976dc tns::MetadataBuilder::InvokeMethod(v8::Localv8::Context, tns::MethodMeta const*, v8::Localv8::Object, tns::V8Args&, std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >, bool) + 76
4 0x104c96ff7 tns::MetadataBuilder::MethodCallback(v8::FunctionCallbackInfov8::Value const&) + 225
5 0x104dea0a1 v8::internal::FunctionCallbackArguments::Call(v8::internal::CallHandlerInfo) + 609
6 0x104de958a v8::internal::MaybeHandlev8::internal::Object v8::internal::(anonymous namespace)::HandleApiCallHelper(v8::internal::Isolate*, v8::internal::Handlev8::internal::HeapObject, v8::internal::Handlev8::internal::HeapObject, v8::internal::Handlev8::internal::FunctionTemplateInfo, v8::internal::Handlev8::internal::Object, v8::internal::BuiltinArguments) + 570
7 0x104de8bad v8::internal::Builtin_Impl_HandleApiCall(v8::internal::BuiltinArguments, v8::internal::Isolate*) + 253
8 0x1056bf7b9 Builtins_CEntry_Return1_DontSaveFPRegs_ArgvOnStack_BuiltinExit + 57
9 0x10565e1f9 Builtins_InterpreterEntryTrampoline + 185
JavaScript stack trace:
at createTimerAndGetId (file: node_modules/@nativescript/core/timer/index.ios.js:45:0)
at setTimeout (file: node_modules/@nativescript/core/timer/index.ios.js:55:0)
at Pinger.reset (file: node_modules/nativescript-mqtt/paho-mqtt.js:759:0)
at LibraryFactory.ClientImpl._socket_send (file: node_modules/nativescript-mqtt/paho-mqtt.js:1501:0)
at LibraryFactory.ClientImpl._process_queue (file: node_modules/nativescript-mqtt/paho-mqtt.js:1200:0)
at LibraryFactory.ClientImpl._schedule_message (file: node_modules/nativescript-mqtt/paho-mqtt.js:1108:0)
at LibraryFactory.ClientImpl.subscribe (file: node_modules/nativescript-mqtt/paho-mqtt.js:949:0)
at Client.subscribe (file: node_modules/nativescript-mqtt/paho-mqtt.js:2103:0)
at subscribe (file: node_modules/nativescript-mqtt/mqtt.js:116:0)
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("", "");
};
I'm sorry this isn't exactly an issue but I wasn't able to get the information over the internet.
Does this plugin allow pure mqtt ? From the documentation : "Currently the library only supports the websocket protocol for cross-platform on port 80 or 443 for SSL."
It is not clear to me if it only supports websocket protocol at all or if it supports both TCP and websockets, with ports limitation on websockets.
Thanks in advance for your answers & for hardwork !
Hi,
Are there any plans to make this work with work with native MQTT packets over TCP/TLS, avoiding the extra websockets wrapper? I'm guessing since NativeScript lacks node's net
module, it might be hard? Would the net-browserify
module help any? https://github.com/emersion/net-browserify
Connection is getting lost after few minutes of active, I tried reinitialising the mqttclient but continously connecting and lost connection as below
'Connection lost: AMQJS0008I Socket closed.'
JS: 'initialized connection'
JS: 'mqtt init called'
JS: 'connecting...'
JS: 'Connection success!'
JS: 'Connection lost: AMQJS0008I Socket closed.'
JS: 'initialized connection'
JS: 'mqtt init called'
JS: 'connecting...'
JS: 'Connection success!'
JS: 'Connection lost: AMQJS0008I Socket closed.'
JS: 'initialized connection'
JS: 'mqtt init called'
JS: 'connecting...'
JS: 'Connection success!'
JS: 'Connection lost: AMQJS0008I Socket closed.'
JS: 'initialized connection'
JS: 'mqtt init called'
JS: 'connecting...'
JS: 'Connection success!'
JS: 'Connection lost: AMQJS0008I Socket closed.'
JS: 'initialized connection'
JS: 'mqtt init called'
JS: 'connecting...'
JS: 'Connection success!'
JS: 'Connection lost: AMQJS0008I Socket closed.'
JS: 'initialized connection'
JS: 'mqtt init called'
JS: 'connecting...'
JS: 'Connection success!'
JS: 'Connection lost: AMQJS0008I Socket closed.'
JS: 'initialized connection'
JS: 'mqtt init called'
JS: 'connecting...'
It is a simple project that connects to EMQ MQTT broker, the connection works but doesnot stay connected no longer than a minute time and even reconnecting it constantly fails
const MQTTCommon = require("nativescript-mqtt/common");
var gencode = function () {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}
module.exports = {
config: {
mqtt_client: null,
surprise: false,
mqtt_username: "",
mqtt_password: "",
topics: [],
mqtt_clientOptions: {
host: "192.168.1.19",
port: 8083,
clientId: gencode(),
path: '/mqtt',
useSSL: false
}
},
init: function (callback) {
console.log('initialized connection');
this.mqtt_clientOptions = this.config.mqtt_clientOptions;
let self = this;
this.mqtt_client = new MQTTClient(this.mqtt_clientOptions);
this.mqtt_client.onConnectionFailure.on(function (err) {
console.log("Connection failed: " + err);
self.init();
});
this.mqtt_client.onConnectionSuccess.on(function () {
console.log("Connection success!");
if (callback) callback();
else self.resubscribe();
});
this.mqtt_client.onConnectionLost.on(function (err) {
console.log("Connection lost: " + err);
self.init();
});
// this.mqtt_client.onMessageArrived.on(function (message) {
// console.log("Message received: " + JSON.parse(message.payload).message);
// });
console.log("mqtt init called");
this.connect();
},
connect: function () {
console.log("connecting...");
try {
this.mqtt_client.connect(this.config.mqtt_username, this.config.mqtt_password);
}
catch (e) {
console.log("Caught error: " + e);
}
},
getClient: function () {
return this.mqtt_client;
},
subscribe: function (topic) {
const self = this;
if (self.mqtt_client.connected) {
console.log('subscribe to ' + topic);
if (self.config.topics.indexOf(topic) == -1) {
try {
self.config.topics.push(topic);
this.mqtt_client.subscribe(topic);
}
catch (e) {
console.log("Caught error: " + e);
}
}
}
},
resubscribe: function () {
const self = this;
if (self.mqtt_client.connected) {
console.log('re-subscribing all');
for (var i = 0; i < this.config.topics.length; i++) {
var element = this.config.topics[i];
try {
this.mqtt_client.subscribe(element);
console.log('resubscribed to ', element);
}
catch (e) {
console.log("Caught error: " + e);
}
}
}
},
publish: function (message) {
console.log("message", message);
// var mqtt_message = new MQTTCommon.Message(message);
try {
console.log('message published to ' + message.topic);
this.mqtt_client.publish(message);
}
catch (e) {
console.log("Caught error: " + e);
}
}
}
How to publish message from app to the mqtt broker?
Simple, just clone the repo, cd demo, tns install, tns run android
below is the error
Found peer TypeScript 2.8.4 references.d.ts:1:22 - error TS6053: File '/Users/karthikbalu/MYDRIVE/DEVELOPMENT/temp/nativescript-mqtt/src/node_modules/tns-platform-declarations/ios.d.ts' not found. 1 /// <reference path="../src/node_modules/tns-platform-declarations/ios.d.ts" /> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ references.d.ts:2:22 - error TS6053: File '/Users/karthikbalu/MYDRIVE/DEVELOPMENT/temp/nativescript-mqtt/src/node_modules/tns-platform-declarations/android.d.ts' not found. 2 /// <reference path="../src/node_modules/tns-platform-declarations/android.d.ts" /> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../src/references.d.ts:1:22 - error TS6053: File '/Users/karthikbalu/MYDRIVE/DEVELOPMENT/temp/nativescript-mqtt/src/node_modules/tns-platform-declarations/ios.d.ts' not found. 1 /// <reference path="./node_modules/tns-platform-declarations/ios.d.ts" /> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../src/references.d.ts:2:22 - error TS6053: File '/Users/karthikbalu/MYDRIVE/DEVELOPMENT/temp/nativescript-mqtt/src/node_modules/tns-platform-declarations/android.d.ts' not found. 2 /// <reference path="./node_modules/tns-platform-declarations/android.d.ts" /> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Executing before-watch hook from /Users/karthikbalu/MYDRIVE/DEVELOPMENT/temp/nativescript-mqtt/demo/hooks/before-watch/nativescript-dev-webpack.js Executing before-shouldPrepare hook from /Users/karthikbalu/MYDRIVE/DEVELOPMENT/temp/nativescript-mqtt/demo/hooks/before-shouldPrepare/nativescript-dev-webpack.js Executing before-prepare hook from /Users/karthikbalu/MYDRIVE/DEVELOPMENT/temp/nativescript-mqtt/demo/hooks/before-prepare/nativescript-dev-typescript.js Hook skipped because either bundling or livesync is in progress. Preparing project... Executing before-prepareJSApp hook from /Users/karthikbalu/MYDRIVE/DEVELOPMENT/temp/nativescript-mqtt/demo/hooks/before-prepareJSApp/nativescript-dev-webpack.js Project successfully prepared (Android) Executing after-prepare hook from /Users/karthikbalu/MYDRIVE/DEVELOPMENT/temp/nativescript-mqtt/demo/hooks/after-prepare/nativescript-dev-webpack.js Executing after-prepare hook from /Users/karthikbalu/MYDRIVE/DEVELOPMENT/temp/nativescript-mqtt/demo/hooks/after-prepare/nativescript-unit-test-runner.js references.d.ts:1:22 - error TS6053: File '/Users/karthikbalu/MYDRIVE/DEVELOPMENT/temp/nativescript-mqtt/src/node_modules/tns-platform-declarations/ios.d.ts' not found. 1 /// <reference path="../src/node_modules/tns-platform-declarations/ios.d.ts" /> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ references.d.ts:2:22 - error TS6053: File '/Users/karthikbalu/MYDRIVE/DEVELOPMENT/temp/nativescript-mqtt/src/node_modules/tns-platform-declarations/android.d.ts' not found. 2 /// <reference path="../src/node_modules/tns-platform-declarations/android.d.ts" /> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../src/references.d.ts:1:22 - error TS6053: File '/Users/karthikbalu/MYDRIVE/DEVELOPMENT/temp/nativescript-mqtt/src/node_modules/tns-platform-declarations/ios.d.ts' not found. 1 /// <reference path="./node_modules/tns-platform-declarations/ios.d.ts" /> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Refreshing application... Successfully synced application org.nativescript.demo on device d1f1ea2a. System.err: android.content.res.Resources$NotFoundException: Resource ID #0xfffffff6 System.err: at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:202) System.err: at android.content.res.Resources.loadXmlResourceParser(Resources.java:2977) System.err: at android.content.res.Resources.getAnimation(Resources.java:2014) System.err: at android.view.animation.AnimationUtils.loadAnimation(AnimationUtils.java:108) System.err: at android.support.v4.app.FragmentManagerImpl.loadAnimation(FragmentManager.java:1102) System.err: at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1600) System.err: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1641) System.err: at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:794) System.err: at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2415) System.err: at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2200) System.err: at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2153) System.err: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2063) System.err: at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:725) System.err: at android.os.Handler.handleCallback(Handler.java:751) System.err: at android.os.Handler.dispatchMessage(Handler.java:95) System.err: at android.os.Looper.loop(Looper.java:154) System.err: at android.app.ActivityThread.main(ActivityThread.java:6823) System.err: at java.lang.reflect.Method.invoke(Native Method) System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563) System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
We just used the demo app provided in this repo
This is a question regarding the package is compatible with web-based applications considering that this is all based on paho-mqtt.js. Currently, there is no maintained vue-mqtt package out there and I tried this library in TNS, maybe it can work for web-based MQTT.
is it possible to use this library to connect with emitter? Emitter does not use user or password and they suggest to just pass null values. However, I'm getting this error while trying that: Invalid type object for userName.
.
If I use the channel and key emitter provides, I get this error: java.net.URISyntaxException: Illegal character in path at index 24: ws://[http://172.23.0.72]:9034
. So they don't replace those values
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.