anthill / 6brain

0 stars 3 forks source link

6brain doesn't receive messages that where sent while it was offline #46

Closed vallettea closed 8 years ago

vallettea commented 8 years ago

This is what I did:

but it is not enough

vallettea commented 8 years ago

Changing for mosquitto with two clients:

"use strict";

var mqtt = require('mqtt');

// var PRIVATE = require('./PRIVATE.json');

var simId="alex1"

var client = mqtt.connect('mqtt://test.mosquitto.org/:1883',
    {
        // username: simId,
        // password: PRIVATE.mqttToken,
        clientId: simId,
        keepalive: 10,
        clean: false,
        // Do not set to a value > 29 until this bug is fixed : https://github.com/mqttjs/MQTT.js/issues/346
        reconnectPeriod: 1000 * 1
    }
);

client.on('connect', function(){
    console.log('connected to the server. ID :', simId);
    client.subscribe('test', {qos:1}); 
});

client.on('message', function(topic, message) {
    // message is a Buffer
    console.log("data received by " + simId + " : " + message.toString());
});

client.on("error", function(error) {
    console.log("ERROR by " + simId + ": ", error);
});

client.on('offline', function() {
    console.log("offline by " + simId + "");
});

client.on('reconnect', function() {
    console.log("reconnect by " + simId + "");
});

and

"use strict";

var mqtt = require('mqtt');

// var PRIVATE = require('./PRIVATE.json');

var simId="alex2"

var client = mqtt.connect('mqtt://test.mosquitto.org/:1883',
    {
        // username: simId,
        // password: PRIVATE.mqttToken,
        clientId: simId,
        keepalive: 10,
        clean: false,
        // Do not set to a value > 29 until this bug is fixed : https://github.com/mqttjs/MQTT.js/issues/346
        reconnectPeriod: 1000 * 1
    }
);

client.on('connect', function(){
    console.log('connected to the server. ID :', simId);
    client.subscribe('test', {qos:1}); 
});

client.on('message', function(topic, message) {
    // message is a Buffer
    console.log("data received by " + simId + " : " + message.toString());
});

client.on("error", function(error) {
    console.log("ERROR by " + simId + ": ", error);
});

client.on('offline', function() {
    console.log("offline by " + simId + "");
});

client.on('reconnect', function() {
    console.log("reconnect by " + simId + "");
});

//start sending
var i = 0;
setInterval(
    function(){
        var message = i.toString();
        console.log("sending ", message)
        client.publish("test", message, {qos: 1}, function(){
            console.log("sent ", message)
        });
        i += 1;
    },
3000)
vallettea commented 8 years ago

with this setup when I reconnect i immediately get all the the messages that where sent during offline perdiod

vallettea commented 8 years ago

This doesn't work with our mosca

vallettea commented 8 years ago

I created a test server on my laptop with

'use strict';

var mosca = require('mosca');
var PRIVATE = require('./PRIVATE.json');

var pubsubsettings = {
  type: 'redis',
  redis: require('redis'),
  db: 12,
  host: "192.168.99.100",
  port: 6379,
  return_buffers: true
};

var moscaSettings = {
  port: 1883,
  backend: pubsubsettings,
  persistence: {
    factory: mosca.persistence.Redis,
    host: "192.168.99.100",
    port: 6379,
  }
};

var server = new mosca.Server(moscaSettings);

server.on('clientConnected', function(client) {
    console.log('Client', client.id, 'connected');
});

server.on('clientDisconnected', function(client) {
    console.log('Client', client.id, 'disconnected');
});

server.on('published', function(packet, client) {
    if (!client)
        client = {id: 'broker'};
    console.log('Client', client.id);
    console.log('published', packet.topic);
});

server.on('delivered', function(packet, client) {
    if (!client)
        client = {id: 'broker'};
    console.log('Client', client.id);
    console.log('received', packet.topic);
});

server.on('subscribed', function(topic, client) {
    console.log('Client', client.id, 'subscribed to', topic);
});

server.on('unsubscribed', function(topic, client) {
    console.log('Client', client.id, 'unsubscribed to', topic);
});

server.on('ready', function(){
    server.authenticate = function (client, username, token, callback) {
        var authorized;
        try {
            authorized = (token.toString() === PRIVATE.token);
        }
        catch(err) {
            console.log('Error in broker authenticator:', err);
            authorized = false;
            callback(err);
        }

        if (authorized)
            callback(null, authorized);
    };
    console.log("ready")
});

and a docker for redis. It works.

vallettea commented 8 years ago

with the 3 previous points properly set if works.