0

I am trying to store references for discovered BLE-Devices inside a Meteor Mongo Collection.

So far the code works fine, but at Devices.insert({....}) an error happens:

Meteor code must always run within a Fiber.

I am using Meteor 1.3.4.1 on Ubuntu 16.04 LTS.

import noble from 'noble';

//Meteor mongo collection
import {Devices} from '../api/devices';

//cleanup collection before start
Devices.remove({});

//store found physical devices and all data
var PhyDevices = [];

noble.on('stateChange', function (state) {

    if(state == 'poweredOn') {

        console.log('scanning...');
        noble.startScanning([], true);
    } else {
        noble.stopScanning;
    }
})


noble.on('discover', function (peripheral){
    addToKnownDevices(peripheral);
});

function addToKnownDevices (peripheral) {
    if(PhyDevices.indexOf(peripheral) == -1){
        PhyDevices.push(peripheral);
        var deviceIndex = PhyDevices.indexOf(peripheral);

        //here is error --> "Meteor code must always run within a Fiber. "
        Devices.insert({
            name: peripheral.advertisement.localName,
            index: deviceIndex
        });

        console.log("Pushed " + peripheral.advertisement.localName + " with index " + deviceIndex);

    }
}

=> Meteor server restarted
I20160708-09:53:18.191(2)? scanning...
W20160708-09:53:18.707(2)? (STDERR) 
W20160708-09:53:18.709(2)? (STDERR) /home/cleitgeb/WebstormProjects/BLEScanner/.meteor/local/build/programs/server/packages/meteor.js:1060
W20160708-09:53:18.709(2)? (STDERR)     throw new Error("Meteor code must always run within a Fiber. " +
W20160708-09:53:18.710(2)? (STDERR)           ^
W20160708-09:53:18.741(2)? (STDERR) Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
W20160708-09:53:18.742(2)? (STDERR)     at Object.Meteor._nodeCodeMustBeInFiber (packages/meteor/dynamics_nodejs.js:9:1)
W20160708-09:53:18.742(2)? (STDERR)     at Object.Meteor.bindEnvironment (packages/meteor/dynamics_nodejs.js:85:1)
W20160708-09:53:18.742(2)? (STDERR)     at addToKnownDevices (imports/scanner.js:42:34)
W20160708-09:53:18.743(2)? (STDERR)     at Noble. (imports/scanner.js:29:5)
W20160708-09:53:18.743(2)? (STDERR)     at Noble.emit (events.js:95:17)
W20160708-09:53:18.743(2)? (STDERR)     at Noble.onDiscover (/home/cleitgeb/WebstormProjects/BLEScanner/node_modules/noble/lib/noble.js:135:10)
W20160708-09:53:18.744(2)? (STDERR)     at [object Object].emit (events.js:106:17)
W20160708-09:53:18.744(2)? (STDERR)     at [object Object].NobleBindings.onDiscover (/home/cleitgeb/WebstormProjects/BLEScanner/node_modules/noble/lib/hci-socket/bindings.js:169:10)
W20160708-09:53:18.744(2)? (STDERR)     at [object Object].emit (events.js:106:17)
W20160708-09:53:18.744(2)? (STDERR)     at [object Object].Gap.onHciLeAdvertisingReport (/home/cleitgeb/WebstormProjects/BLEScanner/node_modules/noble/lib/hci-socket/gap.js:193:10)
=> Exited with code: 8
Kyll
  • 7,036
  • 7
  • 41
  • 64
DirtDiver
  • 117
  • 1
  • 14
  • Instead of `noble.on('discover', function...)` do `noble.on('discover, Meteor.bindEnvironment(function...))` so that this function carries the Fiber environment of Meteor. If this solved your issue, then there's probably some duplicates around the place. – Kyll Jul 08 '16 at 08:15
  • Thank you. It seems to work - i'll work on with my code and i will mark the question as solved later. – DirtDiver Jul 08 '16 at 08:29
  • Well, [this question](http://stackoverflow.com/questions/10192938/) is about the same thing but I'm not really satisfied with the answers. I may go for a canonical there... – Kyll Jul 08 '16 at 08:36
  • Than you for your help - it works fine. Can you publish your first comment as the answer? – DirtDiver Jul 11 '16 at 08:27

1 Answers1

0

The comment from Kyll had the solution inside:

Instead of noble.on('discover', function...) do noble.on('discover, Meteor.bindEnvironment(function...)) so that this function carries the Fiber environment of Meteor. If this solved your issue, then there's probably some duplicates around the place.

Here is mor information about this topic: https://www.eventedmind.com/items/meteor-what-is-meteor-bindenvironment

DirtDiver
  • 117
  • 1
  • 14