-
Notifications
You must be signed in to change notification settings - Fork 5
/
ingest.js
80 lines (74 loc) · 2.1 KB
/
ingest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var pubnub = require('pubnub');
var sequelize = require('sequelize');
var dotenv = require('dotenv');
dotenv.load();
// Initialize connection to database (using environment variables)
var connection = new sequelize('market_orders', process.env.DB_SERVER_USER_NAME, process.env.DB_SERVER_USER_PASSWORD, {
host: process.env.DB_SERVER_HOST,
port: process.env.DB_SERVER_PORT,
dialect: 'postgres',
//operatorsAliases: false,
logging:()=>{}
});
// Define model
var order = connection.define('orders', {
order_time: {
type: sequelize.BIGINT,
allowNull: false,
primaryKey: true
},
trade_type: {
type: sequelize.STRING,
allowNull: false
},
symbol: {
type: sequelize.STRING,
allowNull: false
},
order_quantity: {
type: sequelize.DOUBLE,
allowNull: false
},
bid_price: {
type: sequelize.DOUBLE,
allowNull: false
}
}, {
timestamps: false,
freezeTableName: true
});
// Initialize PubNub client
var pubnub = new pubnub({
ssl: true,
subscribe_key: 'sub-c-4377ab04-f100-11e3-bffd-02ee2ddab7fe'
});
// Subscribe (listen on) to channel
pubnub.subscribe({
channels: ['pubnub-market-orders']
});
// Handle message payload
pubnub.addListener({
message: function (message) {
console.log(message.message);
connection.sync({
//logging: ()=>{}
})
.then(function () {
// Build and Save message stream to database
var orderInstance = order.build({
order_time: message.message.timestamp,
trade_type: message.message.trade_type,
symbol: message.message.symbol,
order_quantity: message.message.order_quantity,
bid_price: message.message.bid_price
})
//orderInstance.save()
//if (Math.random() > .9) {
//throw new Error('Something unusual'+new Date().toISOString())
//}
})
.catch(function (err) {
console.log(err);
});
}
});