Collections insert doesn't work, even with Meteor.bindEnvironment on the server!


#1

HI there, I’m quite new to Meteor, sorry if this is really dumb!

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 have for now called Maindb. 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'

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 poloniexConnection = new autobahn.Connection({
	  url: wsuri,
	  realm: "realm1"
	});

 	poloniexConnection.onopen = Meteor.bindEnvironment(function(session) {

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

		session.subscribe('ticker', tickerEvent);		

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

	});

poloniexConnection.open();

 });

The console logs the data feed, but the insert doesn’t work.

Does anyone know what could be happening ? I tried wrapping the tickerEvent function in the Meteor.bindEnvironment without success.

Thanks!

Alex


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

tickerEvent is fired asynchronously so that should be wrapped. The onopen callback doesn’t need bindEnvironment I think.


#3

Thanks! That is what I had done initially, but when I do that not even the console.log works.


#4

Solution found! The tickerEvent has to simply come outside of the onopen handler, and wrapped in the

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

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 poloniexConnection = new autobahn.Connection({
	  url: wsuri,
	  realm: "realm1"
	});

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

 	poloniexConnection.onopen = function(session) {

		session.subscribe('ticker', tickerEvent);		

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

	}

poloniexConnection.open();

 });

#5

Is it only Collection inserts that need to be wrapped in a fiber? I only get errors around the insert statements.


#6
import Transport from 'autobahn';
import Fiber from 'fibers';
import {Meteor} from 'meteor/meteor';


class WAMPConnection {
  constructor(onOpen, onClose) {
    const url = Meteor.settings.poloniexWAMPURL;

    this.connection = new Transport.Connection({url, realm: 'realm1'});

    this.connection.onopen = (session) => {
      if (Meteor.isDevelopment) {
        Meteor._debug('Connection established');
      }

      this.session = session;
      return onOpen && onOpen(this);
    };

    this.connection.onclose = (reason) => {
      if (Meteor.isDevelopment) {
        Meteor._debug(`Connection closed ${reason}`);
      }

      return onClose && onClose();
    };

    this.connection.open();
  }

  subscribe(name, next) {
    if (this.session) {
      return this.session.subscribe(name, next);
    }
  }

  subscribeTicker(next) {
    if (this.session) {
      return this.session.subscribe('ticker', (args) => {
        const [pairId] = args;

        try {
          const value = parseFloat(args[1]);
          const ask = parseFloat(args[2]);
          const bid = parseFloat(args[3]);
          const trend = parseFloat(args[4]);

          const fiber = Fiber(() => next({pairId, value, ask, bid, trend}));

          return fiber.run();
        } catch (err) {
          throw err;
        }
      });
    }
  }
}

export default WAMPConnection;

// then subscribe anywhere you wish
import WAMPConnection from './connection';

const Connection = new WAMPConnection((connection) => {
    connection.subscribeTicker(({pairId, value}) => {
      iterateBots(pairId, value);
    });
  });

#7

@aadams Yes seems only inserts posed problems, the rest works.

@mrzafod amazing, thank you ! Will try this ASAP