-
Notifications
You must be signed in to change notification settings - Fork 25
/
app.js
45 lines (39 loc) · 1.66 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
express = require('express.io')
app = express().http().io()
var connectionString = process.env.THINGLABS_EVENTHUB_CONNSTRING || '<eventhub connection string>'
// Setup your sessions, just like normal.
app.use(express.cookieParser())
app.use(express.session({secret: 'thinglabs'}))
// Session is automatically setup on initial request.
app.get('/', function(req, res) {
req.session.loginDate = new Date().toString()
res.sendfile(__dirname + '/index.html')
});
app.use(express.static(__dirname + '/static'));
// Instantiate an eventhub client
eventHubClient = require('azure-event-hubs').Client;
var client = eventHubClient.fromConnectionString(connectionString, 'thinglabseventhub')
app.io.route('ready', function(req) {
// For each partition, register a callback function
client.getPartitionIds().then(function(ids) {
ids.forEach(function(id) {
var minutesAgo = 5;
var before = (minutesAgo*60*1000);
client.createReceiver('$Default', id, { startAfterTime: Date.now() - before })
.then(function(rx) {
rx.on('errorReceived', function(err) { console.log(err); });
rx.on('message', function(message) {
console.log(message.body);
var body = message.body;
try {
app.io.broadcast('data', body);
} catch (err) {
console.log("Error sending: " + body);
console.log(typeof(body));
}
});
});
});
});
});
app.listen(process.env.port || 7076)