简体   繁体   中英

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

I am trying to run a Mosca MQTT broker within a Node.js environment with its MongoDB backend in Microsoft's Azure cloud. The DocumentDB storage has a MongoDB API.

First I copied the example code from the Mosca website 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')
}

... which works fine with a locally installed MongoDB server.

Then I replaced the two url: occurrences with the Node.js connection string that can be obtained from the Azure portal. After that, the connection to the Azure DocumentDB failes with the following error:

$ 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)

Any idea what would be missing here?

I don't know for sure, but I assume that Azure DocumentDB does not implement all the features of MongoDB, it just presents the same network API so you can use the existing clients to talk to it.

In this case it doesn't appear to support capped collections (which are basically like circular buffers) that mosca will be using to limit the amount of data that it ends up storing in the database. Mosca is testing the collection it creates and finds it's not setup as it expects and bails out. While you may be able to remove this test the code it will probably mean that you will end up a rapidly growing collection that is expecting the database to automatically remove old records.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM