aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorivanshen <iwshen11@gmail.com>2019-03-05 20:58:03 +0000
committerivanshen <iwshen11@gmail.com>2019-03-05 20:58:03 +0000
commit0a573cc3e918e751f5a9e02f5877f7786636539d (patch)
treee4726c734926e0c2dc4fcf1af3c94367476bb458
parent786d0aacd27f27875568c7a89d9e1df918fadbf3 (diff)
Added live update functionality (feat 8)
-rw-r--r--web/app.js6
-rw-r--r--web/public/javascripts/map.js32
-rw-r--r--web/routes/index.js13
-rw-r--r--web/routes/sse.js15
4 files changed, 61 insertions, 5 deletions
diff --git a/web/app.js b/web/app.js
index f0ae3a5..0dd447d 100644
--- a/web/app.js
+++ b/web/app.js
@@ -19,13 +19,14 @@ app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
+app.use(require('./routes/sse'));
var env = process.env.NODE_ENV || 'development';
var uri;
if (env === 'development') {
- uri = 'mongodb://localhost:27017/helpthehome'
+ // uri = 'mongodb://localhost:27017/helpthehome'
// use for small testing only if really necessary
- // uri = 'mongodb+srv://development:dreamteam@cluster0-krnr4.mongodb.net/helpthehome?retryWrites=true'
+ uri = 'mongodb+srv://development:dreamteam@cluster0-krnr4.mongodb.net/helpthehome?retryWrites=true'
} else if (env === 'qa') {
uri = process.env.MONGODB_URI
} else if (env === 'production') {
@@ -45,7 +46,6 @@ mongoose.connect(uri, { useNewUrlParser: true }, function (error) {
app.use('/', indexRouter);
app.use('/users', usersRouter);
-
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
diff --git a/web/public/javascripts/map.js b/web/public/javascripts/map.js
index 190307d..5739dec 100644
--- a/web/public/javascripts/map.js
+++ b/web/public/javascripts/map.js
@@ -11,7 +11,6 @@ L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/toner-lite/{z}/{x}/{y}{r}
maxZoom: 18,
ext: 'png'
}).addTo(map);
-
L.geoJson(points, {
pointToLayer: function (feature, latlng) {
//return L.circleMarker(latlng);
@@ -21,3 +20,34 @@ L.geoJson(points, {
// layer.feature.geometry gives you access to all the fields
return "<p>" + JSON.stringify(layer.feature.geometry) + "</p>";
}).addTo(map)
+if (!!window.EventSource) {
+ var source = new EventSource('/stream');
+
+ source.addEventListener('message', function(e) {
+ var data = JSON.parse(e.data);
+ if (data.coordinates) {
+ points.push(data);
+ L.geoJson(points, {
+ pointToLayer: function (feature, latlng) {
+ //return L.circleMarker(latlng);
+ return L.marker(latlng);
+ }
+ }).bindPopup(function (layer) {
+ // layer.feature.geometry gives you access to all the fields
+ return "<p>" + JSON.stringify(layer.feature.geometry) + "</p>";
+ }).addTo(map)
+ }
+ }, false)
+
+ source.addEventListener('open', function(e) {
+ console.log("Connection was opened")
+ }, false)
+
+ source.addEventListener('error', function(e) {
+ if (e.readyState == EventSource.CLOSED) {
+ console.log("Connection was closed")
+ }
+ }, false)
+} else {
+ console.log("sse not supported.");
+} \ No newline at end of file
diff --git a/web/routes/index.js b/web/routes/index.js
index b2d0796..6e6b920 100644
--- a/web/routes/index.js
+++ b/web/routes/index.js
@@ -2,7 +2,7 @@ var express = require('express');
var router = express.Router();
var Mockpoints = require('../models/mockpoints');
var Points = require('../models/points');
-
+const connections = [];
/* GET home page. */
router.get('/', function(req, res, next) {
// for now just redirect to /map
@@ -11,6 +11,7 @@ router.get('/', function(req, res, next) {
/* GET Map page. */
router.get('/map', function(req,res) {
+ // Stream.emit("push", "test", { msg: "admit one" });
// load the map with all the points
// Mockpoints.get_points(function(err, points){
Points.get_points(function(err, points){
@@ -36,10 +37,20 @@ router.post('/mobilerequest', function(req, res) {
console.log(`err inserting mobile request into db: ${err}`);
res.status(500).send({ error: "boo:(" });
} else {
+ for(var i = 0; i < connections.length; i++) {
+ connections[i].sseSend(data);
+ }
console.log(result);
res.status(200).send({'status': 'success', 'data': data});
}
});
+
});
+
+router.get('/stream', function(req, res){
+ res.sseSetup()
+ res.sseSend('ok');
+ connections.push(res);
+});
module.exports = router;
diff --git a/web/routes/sse.js b/web/routes/sse.js
new file mode 100644
index 0000000..5cd9a3f
--- /dev/null
+++ b/web/routes/sse.js
@@ -0,0 +1,15 @@
+module.exports = function (req, res, next) {
+ res.sseSetup = function() {
+ res.writeHead(200, {
+ 'Content-Type': 'text/event-stream',
+ 'Cache-Control': 'no-cache',
+ 'Connection': 'keep-alive'
+ })
+ }
+
+ res.sseSend = function(data) {
+ res.write("data: " + JSON.stringify(data) + "\n\n");
+ }
+
+ next()
+} \ No newline at end of file