forked from linus-berg/IBM-Application
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
31 lines (26 loc) · 1.19 KB
/
app.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
var express = require('express');
var app = express();
var http = require('http').Server(app);
var ws = require("socket.io")(http);
var cfenv = require('cfenv');
var IoTApp = require('./application/application.js');
/* Serve the files out of ./public as our main files. */
app.use(express.static(__dirname + '/public'));
/*
Get the app environment from Cloud Foundry,
if you are developing locally (VCAP_SERVICES environment variable not set),
cfenv will use the file defined by vcapFile instead.
You can export these local json files from IBM Cloud!
*/
var app_env = cfenv.getAppEnv({vcapFile: 'vcap.json'});
const IOT_PLATFORM = "Matterhorn";
/* Retrieve Cloud Foundry environment variables. */
var credentials = app_env.getServiceCreds(IOT_PLATFORM);
var application = new IoTApp(credentials.org, credentials.apiKey, credentials.apiToken);
/* Application is an event emitter, so we listen for the payload event we defined in application.js! */
application.on('payload', function(data) {
/* We then broadcast to our clients. */
ws.emit('broadcast', JSON.parse(data).temp);
});
/* Start server on the specified port and binding host app_env.port */
http.listen(app_env.port || 4096, function() {});