Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
124 lines (99 loc) · 3.83 KB
/
Copy pathserver.js
File metadata and controls
124 lines (99 loc) · 3.83 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
Copyright 2016 Streamdata.io
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// add EventSource dependency
var EventSource = require('eventsource');
// add json patch dependency
var JsonPatch = require('fast-json-patch');
// add http dependency
var Http = require('http');
function server() {
// define variables
var eventSource = null;
// initialize data
var data = [];
function connect() {
// building the URL to get your API streamed as follow:
// targetUrl is the JSON API you wish to stream
// you can use this example API which simulates updating stocks prices from a financial market
var targetUrl = 'http://stockmarket.streamdata.io/prices';
// appToken is the way Streamdata.io authenticates you as a valid user.
// you MUST provide a valid token for your request to go through.
var appToken = 'NDQ1Zjk0NTAtZTE0Ny00MmU0LTljNGEtMDczMTEwMGZkMTQ0';
// finally the url you will request is composed as follow
// the call to your target API is made through https://streamdata.motwin.net/ proxy
var url = 'https://streamdata.motwin.net/' + targetUrl + '?X-Sd-Token=' + appToken;
// simply use the eventsource API to get connected
var eventSource = new EventSource(url);
// add callbacks to react to EventSource events
// the standard 'open' callback will be called when connection is established with the server
eventSource.addEventListener('open', function() {
console.log("connected!");
});
// the standard 'error' callback will be called when an error occur with the evenSource
// for example with an invalid token provided
eventSource.addEventListener('error', function(e) {
console.log('ERROR!', e);
eventSource.close();
});
// the streamdata.io specific 'data' event will be called when a fresh Json data set
// is pushed by Streamdata.io coming from the API
eventSource.addEventListener('data', function(e) {
console.log("data: \n" + e.data);
// memorize the fresh data set
data = JSON.parse(e.data);
forwardData();
});
// the streamdata.io specific 'patch' event will be called when a fresh Json patch
// is pushed by streamdata.io from the API. This patch has to be applied to the
// latest data set provided.
eventSource.addEventListener('patch', function(e) {
// display the patch
console.log("patch: \n" + e.data);
// apply the patch to data using json patch API
JsonPatch.apply(data, JSON.parse(e.data));
forwardData();
});
};
function forwardData() {
const reqData = JSON.stringify(data);
const options = {
// Define here the hostname you want to forward the updated data to
hostname: '192.168.2.177',
// Add the port
port: 1234,
// Add the path
path: '/',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
const req = Http.request(options, (res) => {
var body = [];
console.log(`STATUS: ${res.statusCode}`);
res.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
console.log(body);
}).on('error', (err) => {
console.error(err.stack);
});
});
req.write(reqData);
req.end();
}
connect();
}
console.log('starting');
server();