Introvertuous/react-native-mqtt

no concurrent publisher support

Aakash007-ai opened this issue · 0 comments

Hello , i am facing an issue for concurrent user. I am not able to send data to broker if one publisher is already connected.

here is my configuration

import init from 'react_native_mqtt';
import AsyncStorage from '@react-native-async-storage/async-storage';
import DeviceInfo from 'react-native-device-info';

init({
size: 10000,
storageBackend: AsyncStorage,
defaultExpires: 1000 * 3600 * 24,
enableCache: true,
sync: {},
});

const defaultConnectOptions = {
reconnect: false,
cleanSession: true,
mqttVersion: 3,
keepAliveInterval: 60,
timeout: 60
}

class MQTTConnection {
constructor(host, port, clientID) {
this.mqtt = new Paho.MQTT.Client(host, port, clientID);
this.QOS = 1;
this.RETAIN = true;
this.channel = 'test';
this.clientID = "device";
//this.clientID = 'new_mqtt_test'
}

connect(host, port, options = null) {
    this.clientID = DeviceInfo.getBrand();

    if (options) {
        this.QOS = options.qos;
        this.RETAIN = options.retain;
    }

    let currentTime = new Date();
    // let clientID = currentTime + uuid.v1();
    // clientID = clientID.slice(0, 23);
    // console.log('clientID: ', clientID)

 
    this.mqtt.onConnectionLost = (res) => {
        this.onMQTTLost;
        console.log("this.mqtt.onConnectionLost"+res)
    };
    this.mqtt.onMessageArrived = (message) => {
        this.onMQTTMessageArrived(message);
    };
    this.mqtt.onMessageDelivered = (message) => {
        this.onMQTTMessageDelivered(message);
    };

    const connectOptions = options ? options : defaultConnectOptions;

    console.log("calling connect")


    this.mqtt.connect({
        onSuccess: ()=>{
            console.log('debug inside connect success .......')
        },
        onFailure: ()=>{
            console.log('debug inside connect failure.....')
        },
        ...connectOptions
    });
}

onMQTTSuccess = () => {
    this.onMQTTConnect()
}

onMQTTFailure = () => {
    this.onMQTTLost()
}

subscribeChannel(channel) {
    console.log('MQTTConnection subscribeChannel: ', channel)
    if (!this.mqtt || !this.mqtt.isConnected()) {
        return;
    }
    this.mqtt.subscribe(channel, this.QOS);
}

unsubscribeChannel(channel) {
    console.log('MQTTConnection unsubscribeChannel: ', channel)
    if (!this.mqtt || !this.mqtt.isConnected()) {
        return;
    }
    this.mqtt.unsubscribe(channel);
}

send(payload) {
    console.log('MQTTConnection send: ')
    if (!this.mqtt || !this.mqtt.isConnected()) {
        return;
    }


    if (!this.channel || !payload) {
        return false;
    }
    
    let now = new Date()
    let options = { timeZone: 'Asia/Kolkata' };
    let currentTimeIST = now.toLocaleString('en-US', options);

    //let utcString = now.toUtring();
    payload = `[${this.clientID}][${currentTimeIST}] : ${payload} `

    console.log(payload)
    
    //console.log(`MQTTConnection send publish channel: ${channel}, payload: ${payload} qos: ${this.QOS} retained: ${this.RETAIN}`)
    this.mqtt.publish(this.channel, payload, this.QOS, this.RETAIN);
}

close() {
    this.mqtt && this.mqtt.disconnect();
    this.mqtt = null;
}

set userID (newID) {
    this.clientID = newID;
}

}

// let client = null

// if (!client){
// client = new MQTTConnection('13.233.185.239', 8083,'ClientId');
// // client = new MQTTConnection('13.233.185.239', 8083,'ClientId');
// client.connect()
// }
//const client= new MQTTConnection('13.233.185.239', 8083,'ClientId');

export default MQTTConnection;

MQTTConnection.prototype.onMQTTConnect = ()=>{console.log('debug connect....')}
MQTTConnection.prototype.onMQTTLost = ()=>{console.log('debug lost...')}
MQTTConnection.prototype.onMQTTMessageArrived = ()=>{console.log('debug arrived...')}
MQTTConnection.prototype.onMQTTMessageDelivered = ()=>{console.log('debug delivered...')}