-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
78 lines (64 loc) · 2.22 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
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
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var http = require('http').Server(app);
var routes = require('./routes/queue');
var io = require('socket.io')(http);
var sqs = require('./lib/sqs');
var user = require('./routes/user');
var dynamo = require('./lib/dynamo');
var messages = require('./routes/messages');
require('events').EventEmitter.defaultMaxListeners = Infinity;
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.use('/queue', routes);
app.use('/api', user, messages)
http.listen(3000, function(){
console.log('listening on *:3000');
});
var queueUrl;
sqs.getQueue(function(e, body) {
if (e) {
return e;
};
queueUrl = body.QueueUrl;
});
io.on('connection', function(socket){
console.log('user connected');
setInterval(function () {
sqs.receiveMessage(queueUrl, function(e, body) {
if (e) {
console.log(e);
};
var receiptHandles = [];
if (body.Messages && body.Messages.length > 0) {
for (var i = 0; i < body.Messages.length; i++) {
receiptHandles.push({ Id : i.toString(), ReceiptHandle : body.Messages[i].ReceiptHandle });
console.log('body', body.Messages[i].Body);
dynamo.putMessage(body.Messages[i]);
io.emit('messages', body.Messages[i]);
}
}
if (receiptHandles.length > 0) {
console.log('delete')
sqs.deleteMessages(receiptHandles, queueUrl, function(e, delBody) {
if(delBody.Successful) {
console.log("Deletion of " + delBody.Successful.length + " succeeded, ");
}
if(delBody.Failed) {
console.log("Deletion of " + delBody.Failed.length + " failed");
}
});
}
});
}, 5000);
});