Mosca MQTT broker (node.js) with MongoDB backend in Azure Cl



我正在尝试在Node.js环境中运行Mosca MQTT代理,并在Microsoft的Azure云中运行MongoDB后端。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服务器。

然后,我将这两个url:匹配项替换为可从 Azure 门户获取的 Node.js 连接字符串。之后,与 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)

知道这里会缺少什么吗?

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

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

最新更新