eclipse / paho.mqtt.javascript

paho.mqtt.javascript
Other
1.15k stars 467 forks source link

JavaScript Client - New Feature Plan : Automatic Reconnect and Disconnected Buffering #48

Closed miketran78727 closed 6 years ago

miketran78727 commented 8 years ago

Automatic Reconnect and Disconnected Publishing Plan

Currently, the Paho JavaScript client is lacking two major areas of functionality: Automatic Reconnect and Disconnected (or Offline) Publishing.

The goal is to implement these features in time for the next release Neon.

This issue aims to outline the plan for adding this new functionality into the JavaScript client. The design and implementation will be closely matched to the Java client's plan

Recap: Possible Client States

There are 5 main potential states that the client can be in. The User will usually desire the client to either be in the connected or disconnected states.

Will automatically attempt to reconnect to the broker (or one of the servers in the host list) while the client is in disconnected state.

Will allow the client to save messages in a buffer whilst the client is in the disconnected state.

The following optional attributes will be added to the Connection Options object:

The following new callback will be added:

The existing onConnectionLost callback will be changed as follows:

The following optional attributes will be added to the Messaging Client object:

The following methods will be added:

When the buffer is full and deleteOldestBufferedMessages is not present or False, the following error will be thrown:

This application wants to use both Automatic Reconnect and Disconnected Publising. The Application does not want to persist buffered messages.

    var client;
    var host = "iot.eclipse.org";
    var port = 1883;
    var clientId = "sample-id";
    var sentMsgs = 0;

    // This function creates a new Messaging client and initiates a connect request.
    function doConnect() {
        client = new Messaging.Client(server, port, clientId);
        client.onConnected = onConnected;            // Callback when connected
        client.onConnectionLost = onConnectionLost;  // Callback when lost connection
        client.disconnectedPublishing = true;        // Enable disconnected publishing
        client.disconnectedBufferSize = 100;         //  Buffer size : 100

        client.connect(
            {
                cleanSession : false, 
                onSuccess : onConnectSuccess, 
                onFailure : onFailedConnect, 
                keepAliveInterval: 30, 
                reconnect : true,         // Enable automatic reconnect
                reconnectInterval: 10     // Reconnect attempt interval : 10 seconds
            }
        );
    }

    // This function is called when the initial connect request is successful.
    function onConnectSuccess(resObj) {
        console.log("Initial connect request succeeded.");
    }

    // This function is called when the intial connect request failed.
    function onFailedConnect(err) {
        console.log("Initial connect request failed. Error message : " + err.errorMessage); 
    }

    // This function is called for all successful connection
    function onConnected(resObj) {
        console.log("Connected to " + resObj.uri);
        if (resObj.reconnect) {
            console.log("It was a result of automatic reconnect.");
        }
    }

    // This function is called when the connection is lost.
    function onConnectionLost(resObj) {
        console.log("Lost connection to " + resObj.uri + "\nError code: " + resObj.errorCode + "\nError text: " + resObj.errorMessage);
        if (resObj.reconnect) {
            console.log("Automatic reconnect is currently active.");
        } else {
            alert("Lost connection to host.");.
        }
    }

    function doSend() {
        // Starts timer to send a message every second
        publishTimer = setInterval(function () {sendTimer()}, 1000);
    }

    function sendTimer() {
        var message = new Messaging.Message("Test Message " + sentMsgs);
        message.destinationName = "/test/topic1";
        message.qos = 1;
        try {
            client.send(message);
            sentMsgs++;
            if (client.bufferedMessages > 0) {
                console.log("Buffered messages : " + client.bufferedMessages);
            }
        } catch (err) {
            console.log("Error occured after " + sentMsgs + " messages sent. Error : " + err.message);
        }
    }
Fabryprog commented 8 years ago

+1

Dovitch commented 8 years ago

exactly what I need! As we say here:"ça gère grave la fougère!"

seesiva commented 8 years ago

Is this still open or part of neon ?

miketran78727 commented 8 years ago

@seesiva You can check out the pull request. https://github.com/eclipse/paho.mqtt.javascript/pull/72 This is still under review.

jpwsutton commented 7 years ago

The PR was merged in manually here: https://github.com/eclipse/paho.mqtt.javascript/commit/258524896c90139e55c9d26d9376e25536b6149d

@miketran78727 could you check that this new functionality works correctly in the develop branch before we close this off please? Just to make sure that my merge went properly :)

miketran78727 commented 7 years ago

@jpwsutton Will do!

miketran78727 commented 7 years ago

@jpwsutton Sorry James.. I have been busy with work in the last few weeks. I will find some gap this week to run my manual tests for the develop branch.

miketran78727 commented 7 years ago

@jpwsutton Offline publishing does not work as expected, My test expects the number of delivered messages = number of published messages after successful reconnect

Messages sent: 57
Messages delivered: 41

I will look into the problem.

miketran78727 commented 7 years ago

I forgot to set the offline publishing properties in the previous test:

        client.disconnectedPublish = true;
        client.disconnectedBufferSize = disconnectedBufferSize;

So, the code is working as expected. If reconnect = true but client.disconnectedPublishing = false, only automatic reconnect is enabled.

celalo commented 7 years ago

When can we expect this one to be released?

ghost commented 7 years ago

hy mike,

the application using node or native javascript?

cjy37 commented 6 years ago

+1

benydc commented 6 years ago

@jpwsutton how can I use this feature for auto-reconnect?

gautam-bmdi commented 5 years ago

@miketran78727 @celalo

Is this feature available now?

Talbot3 commented 5 years ago

@gautam-bmdi when I use it , test error. I not found it on master . come back to use mqtt.js