octoblu / meshblu

Meshblu is a cross-protocol IoT machine-to-machine messaging system.
https://meshblu.readme.io/
MIT License
816 stars 181 forks source link

Subscriptions are not working (with NeDB) #67

Closed fokusferit closed 9 years ago

fokusferit commented 10 years ago

Hi,

i'm having some issues with the subscription REST-API. I've created some devices and i want to subscribe to one specific device. My curl Call is the following:

curl -X GET "http://localhost:3000/subscribe/0cbd6501-1bca-11e4-834e-331a925ae32b" --header "skynet_auth_uuid: d60670c1-1bbe-11e4-834e-331a925ae32b" --header "skynet_auth_token: 06i10tq9lu7x8ncdixm6uqp0oqykqpvi"

The response from meshblu is: {"code":"InternalError","message":"Object # has no method 'pipe'"} .

Currently, i'm not able to use subscriptions anyway. Websocket are also not working -> Response: timeout.

But the subscriptions should work as the Demo on skynet.im is using subscriptions and working fine.

Any ideas?

=============EDIT====================================================

My first "investigations" are showing that there is an issue with using .pipe() method in "setupHttpRoutes.js". The db-query returns a Cursor-Object (subscribe.js) and this object has only the following structure:

-Cursor -- db: Datastore --- autoload: true --- executor: Executor --- filename: "..." --- inMemoryOnly: false --- indexes: Object --- persistence: Persistence -- execFn: function (err, docs, callback) { -- query: Object --- $or: Array[2] --- timestamp: Object

As you can see, the call "subscribe(req.params.uuid).pipe(foo)" in setupHttpRoutes.js is not possible since there is no such function returned.

Will try to use mongodb, maybe they are returning something

fokusferit commented 10 years ago

Sorry for the additional post, but i think i found one Bug which is related to subscriptions and using mongodb.

With the current implementation, i'm getting this error "tailable cursor requested on non capped collection ..." each time i want to subscribe to an device. I was able to fix this issue in the databse.js with creating the database collection directly with the "capped: true" property. Without this option it is not possible to use tailable cursors". I don't know if this is related to some changes in mongodb (tested version: 2.6.3). I will make a Pull-Request for this.

Unfortunately, i was not able to fix the issue with NeDB.

chrismatthieu commented 10 years ago

Thanks for the heads-up. That bug is on our list (Basecamp #212).

Subscribe should work with MQTT and WebSocket protocols. We are planning to redirect HTTP and CoAP subscriptions to Redis rather than tailing Mongo. This may still be an issue for Raspberry Pis and other smaller devices that may not have enough processing power to run Redis.

fokusferit commented 10 years ago

Ok, thanks for the info! And for using subscriptions with Mongo, i think i've fixed that with adding the following to database.js:

if(config.mongo){

  var mongojs = require('mongojs');
  var db = mongojs(config.mongo.databaseUrl);
  var devices = db.createCollection('devices' , {capped: true, size:10000,max:1000}, function() {
    // do something here
    console.log("Devices collection created");
  });
  var events = db.createCollection('events' , {capped: true, size:10000,max:1000}, function() {
    // do something here
    console.log("Events collection created");
  });
  var data = db.createCollection('data' , {capped: true, size:10000,max:1000}, function() {
    // do something here
    console.log("Data collection created");
  });
  module.exports = {
    devices: db.collection('devices'),
    events: db.collection('events'),
    data: db.collection('data')
  };

} 

For now, it is working fine for me (WebSocket subscriptions tested).

chrismatthieu commented 10 years ago

Cool. You should only need to cap the events collection.