Sunday, 30 April 2017

Save tick data to mysql database

I getting from a push service trading data via nodejs.

I would like to save it directly in my mysql database. Below you can find my current script:

var mysql = require('mysql')

//DB
var connection = mysql.createConnection({
  host: 'localhost',
  user: 'pisco',
  password: '',
  database: 'c9'
});

connection.connect(function(err) {
  if (!err) {
    console.log("Database is connected ... nn");
  }
  else {
    console.log("Error connecting database ... nn");
  }
});


var autobahn = require('autobahn');
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);
    var query = connection.query('INSERT INTO cryptocurrency SET ?', args,
      function(err, result) {
        if (err, result) {
          console.log(err.message);
        }
        else {
          console.log(result);
        }
      });
    console.log(query.sql);
  }
  session.subscribe('ticker', tickerEvent);
}

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

connection.open();

Any suggestions what I am doing wrong?

I appreciate your replies!



via mrquad

No comments:

Post a Comment