forked from bloomberg/blpapi-http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarketDataSubscription_socketio.js
More file actions
61 lines (50 loc) · 1.56 KB
/
Copy pathMarketDataSubscription_socketio.js
File metadata and controls
61 lines (50 loc) · 1.56 KB
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
var fs = require('fs');
var util = require('util');
var io = require('socket.io-client');
var opts = require('./cmdline').parse(); // Parse cmdline options.
var N_DATA_EVENTS = 2; // Number of subscription data received before unsubscribe
var SUBSCRIPTIONS = [
{ security: 'AAPL US Equity', correlationId: 0, fields: ['LAST_PRICE'] },
{ security: 'GOOG US Equity', correlationId: 1, fields: ['LAST_PRICE'] }
];
// main
var ns = 'subscription';
var url = util.format('%s:%s/%s', opts.host, opts.port, ns);
var opt = {
secure: true,
cert: fs.readFileSync('client.crt'),
key: fs.readFileSync('client.key'),
ca: fs.readFileSync('bloomberg.crt')
};
var socket = io.connect(url, opt);
socket.on('connect', function () {
console.log('Connected');
socket.emit('subscribe', SUBSCRIPTIONS);
});
var data_events_count = 0;
socket.on('data', function (data) {
console.log(data);
if (++data_events_count === N_DATA_EVENTS) {
socket.emit('unsubscribe');
}
});
socket.on('subscribed', function (correlationIds) {
console.log('Subscribed:', correlationIds);
});
var unsubscribe_count = 0;
socket.on('unsubscribed', function (correlationIds) {
console.log('Unsubscribed:', correlationIds);
unsubscribe_count += correlationIds.length;
if (SUBSCRIPTIONS.length === unsubscribe_count) {
socket.disconnect();
}
});
socket.on('disconnect', function() {
console.log('Socket Disconnected.');
process.exit();
});
socket.on('err', function (data) {
console.log(data);
socket.disconnect();
process.exit();
});