Meteor insert collection within server callback not working

87 views Asked by At

Basically, I am trying to get a wss feed going from Poloniex, and update a collection with it so that I can have 'latest' prices in a collection (I will update and overwrite existing entries) and show it on a web page. For now, I got the wss working and am just trying to insert some of the data in the collection to see if it works, but it doesn't and I can't figure out why!

Note: The collection works, I've manually inserted a record with the shell.

Here is the code I have now:

import { Meteor } from 'meteor/meteor';
import * as autobahn from "autobahn";
import { Mongo } from 'meteor/mongo'
import { SimpleSchema } from 'meteor/aldeed:simple-schema'

//quick DB 
Maindb = new Mongo.Collection('maindb');

Maindb.schema = new SimpleSchema({
  place: {type: String},
  pair: {type: String},
  last: {type: Number, defaultValue: 0}
});

Meteor.startup(() => {
  var wsuri = "wss://api.poloniex.com";
  var Connection = new autobahn.Connection({
  url: wsuri,
  realm: "realm1"
  });

  Connection.onopen = function(session) 
  {

    function tickerEvent (args,kwargs) {
        console.log(args[0]);
        Maindb.insert({place: 'Poloniex', pair: args[0]});
    }

    session.subscribe('ticker', tickerEvent);


    Connection.onclose = function () {
        console.log("Websocket connection closed");
    }

}

Connection.open();

});

The console logs the feed but then the insert does not work. I looked online and it said that to get an insert to work when in a 'non Meteor' function, you need to use Meteor.bindEnvironment which I did:

I changed

    function tickerEvent (args,kwargs) {
        console.log(args[0]);
        Maindb.insert({place: 'Poloniex', pair: args[0]});
    }

which became

    var tickerEvent = Meteor.bindEnvironment(function(args,kwargs) {
        console.log(args[0]);
        Maindb.insert({place: 'Poloniex', pair: args[0]});
    }); tickerEvent(); 

Which doesn't do anything - not even print the feed on my console. Using this same structure but simply removing Meteor.bindEnvironmentprints again to the console but doesn't update.

Am I doing something wrong?

0

There are 0 answers