在Azure Cloud中使用MongoDB后端的Mosca MQTT代理(node.js)

时间:2017-01-25 15:44:54

标签: node.js mongodb azure mqtt mosca

我正在尝试在Node.js环境中运行一个Mosca MQTT代理,其MongoDB后端位于Microsoft的Azure云中。 DocumentDB存储有一个MongoDB API。

首先,我复制了Mosca网站https://github.com/mcollina/mosca/wiki/Mosca-basic-usage#lets-put-it-all-together-now

中的示例代码
var mosca = require('mosca')

var ascoltatore = {
    type: 'mongo',        
    url: 'mongodb://localhost:27017/mqtt',
    pubsubCollection: 'ascoltatori',
    mongo: {}
};

var moscaSettings = {
    port: 1883,
    backend: ascoltatore,
    persistence: {
        factory: mosca.persistence.Mongo,
        url: 'mongodb://localhost:27017/mqtt'
    }
};

var server = new mosca.Server(moscaSettings);
server.on('ready', setup);

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

server.on('published', function(packet, client) {
    console.log('Published', packet.payload);
});

function setup() {
    console.log('Mosca server is up and running')
}

...适用于本地安装的MongoDB服务器。

然后我用可以从Azure门户获得的Node.js连接字符串替换了两个url:次出现。 之后,与Azure DocumentDB的连接会出现以下错误:

$ node index.js 
/[...]/node_modules/mongodb/lib/utils.js:98
process.nextTick(function() { throw err; });
                              ^
Error: Cannot recover. Collection is not capped.
at /[...]/node_modules/ascoltatori/lib/mongo_ascoltatore.js:241:26
at handleCallback (/[...]/node_modules/mongodb/lib/utils.js:95:56)
at /[...]/node_modules/mongodb/lib/collection.js:1559:5
at handleCallback (/[...]/node_modules/mongodb/lib/utils.js:95:56)
at /[...]/node_modules/mongodb/lib/collection.js:1528:5
at handleCallback (/[...]/node_modules/mongodb/lib/utils.js:95:56)
at /[...]/node_modules/mongodb/lib/cursor.js:852:16
at handleCallback (/[...]/node_modules/mongodb-core/lib/cursor.js:171:5)
at setCursorDeadAndNotified (/[...]/node_modules/mongodb-core/lib cursor.js:506:3)
at nextFunction (/[...]/node_modules/mongodb-core/lib/cursor.js:652:7)
at Cursor.next [as _next] (/[...]/node_modules/mongodb-core/lib cursor.js:693:3)
at fetchDocs (/[...]/node_modules/mongodb/lib/cursor.js:848:10)
at /[...]/node_modules/mongodb/lib/cursor.js:871:7
at handleCallback (/[...]/node_modules/mongodb-core/lib/cursor.js:171:5)
at nextFunction (/[...]/node_modules/mongodb-core/lib/cursor.js:683:5)
at /[...]/node_modules/mongodb-core/lib/cursor.js:594:7
at queryCallback (/[...]/node_modules/mongodb-core/lib/cursor.js:253:5)
at /[...]/node_modules/mongodb-core/lib/connection/pool.js:457:18
at nextTickCallbackWith0Args (node.js:419:9)
at process._tickCallback (node.js:348:13)

知道这里会遗漏什么?

1 个答案:

答案 0 :(得分:0)

我不确定,但我认为Azure DocumentDB没有实现MongoDB的所有功能,它只是提供相同的网络API,因此您可以使用现有的客户端与之交谈。

在这种情况下,它似乎不支持capped collections(基本上类似于循环缓冲区),mosca将使用它来限制它最终存储在数据库中的数据量。 Mosca正在测试它创建的集合,并发现它没有像预期的那样进行设置并挽救。虽然您可能能够删除此测试代码,但这可能意味着您将结束快速增长的集合,期望数据库自动删除旧记录。