From 0a573cc3e918e751f5a9e02f5877f7786636539d Mon Sep 17 00:00:00 2001 From: ivanshen Date: Tue, 5 Mar 2019 15:58:03 -0500 Subject: Added live update functionality (feat 8) --- web/app.js | 6 +++--- web/public/javascripts/map.js | 32 +++++++++++++++++++++++++++++++- web/routes/index.js | 13 ++++++++++++- web/routes/sse.js | 15 +++++++++++++++ 4 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 web/routes/sse.js (limited to 'web') 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 "

" + JSON.stringify(layer.feature.geometry) + "

"; }).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 "

" + JSON.stringify(layer.feature.geometry) + "

"; + }).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 -- cgit v1.2.3 From 3f5bea8fd49106249272e5f0f9158cbcd76c89a4 Mon Sep 17 00:00:00 2001 From: Ivan Shen Date: Tue, 5 Mar 2019 16:12:26 -0500 Subject: Point back to local db --- web/app.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'web') diff --git a/web/app.js b/web/app.js index 0dd447d..9d6e9d0 100644 --- a/web/app.js +++ b/web/app.js @@ -24,9 +24,9 @@ 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') { -- cgit v1.2.3 From 9f5447a54ba8bbbcc271035d180822efedce6670 Mon Sep 17 00:00:00 2001 From: Ivan Shen Date: Tue, 5 Mar 2019 16:13:00 -0500 Subject: Remove commented code --- web/routes/index.js | 1 - 1 file changed, 1 deletion(-) (limited to 'web') diff --git a/web/routes/index.js b/web/routes/index.js index 6e6b920..885724d 100644 --- a/web/routes/index.js +++ b/web/routes/index.js @@ -11,7 +11,6 @@ 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){ -- cgit v1.2.3 From 3b36b1ad459ee739a87a32a18bb5846770125bec Mon Sep 17 00:00:00 2001 From: ivanshen Date: Tue, 5 Mar 2019 16:39:35 -0500 Subject: modularize plot points --- web/app.js | 2 +- web/public/javascripts/map.js | 32 ++++++++++++++------------------ web/routes/index.js | 6 +++++- 3 files changed, 20 insertions(+), 20 deletions(-) (limited to 'web') diff --git a/web/app.js b/web/app.js index 9d6e9d0..1bb97d8 100644 --- a/web/app.js +++ b/web/app.js @@ -26,7 +26,7 @@ var uri; if (env === 'development') { 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') { diff --git a/web/public/javascripts/map.js b/web/public/javascripts/map.js index 5739dec..2547780 100644 --- a/web/public/javascripts/map.js +++ b/web/public/javascripts/map.js @@ -3,6 +3,18 @@ // attribution: '© OpenStreetMap contributors' // }).addTo(map); +function plotPointsOnMap(points) { + 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 "

" + JSON.stringify(layer.feature.geometry) + "

"; + }).addTo(map) +} + // different basemap L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/toner-lite/{z}/{x}/{y}{r}.{ext}', { attribution: 'Map tiles by Stamen Design, CC BY 3.0 — Map data © OpenStreetMap contributors', @@ -11,15 +23,7 @@ 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); - return L.marker(latlng); - } -}).bindPopup(function (layer) { - // layer.feature.geometry gives you access to all the fields - return "

" + JSON.stringify(layer.feature.geometry) + "

"; -}).addTo(map) +plotPointsOnMap(points); if (!!window.EventSource) { var source = new EventSource('/stream'); @@ -27,15 +31,7 @@ if (!!window.EventSource) { 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 "

" + JSON.stringify(layer.feature.geometry) + "

"; - }).addTo(map) + plotPointsOnMap(points); } }, false) diff --git a/web/routes/index.js b/web/routes/index.js index 885724d..b2ba91f 100644 --- a/web/routes/index.js +++ b/web/routes/index.js @@ -36,6 +36,7 @@ router.post('/mobilerequest', function(req, res) { console.log(`err inserting mobile request into db: ${err}`); res.status(500).send({ error: "boo:(" }); } else { + // send event to all connections for(var i = 0; i < connections.length; i++) { connections[i].sseSend(data); } @@ -48,8 +49,11 @@ router.post('/mobilerequest', function(req, res) { router.get('/stream', function(req, res){ - res.sseSetup() + // set up server side event (communication line between front end and server) + res.sseSetup(); + // send an event to the front end to open the connection res.sseSend('ok'); + // push connection to connection array connections.push(res); }); module.exports = router; -- cgit v1.2.3 From b70aaf5168dd60da1d4087962b7250d584b0936f Mon Sep 17 00:00:00 2001 From: Ivan Shen Date: Tue, 5 Mar 2019 16:38:48 -0500 Subject: Comment out prod db --- web/app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'web') diff --git a/web/app.js b/web/app.js index 1bb97d8..9d6e9d0 100644 --- a/web/app.js +++ b/web/app.js @@ -26,7 +26,7 @@ var uri; if (env === 'development') { 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') { -- cgit v1.2.3 From a5ff29d43a952ebe2fcc18181a01beda0e5d62d2 Mon Sep 17 00:00:00 2001 From: Ivan Shen Date: Tue, 5 Mar 2019 16:41:47 -0500 Subject: remove unnecessary !! --- web/public/javascripts/map.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'web') diff --git a/web/public/javascripts/map.js b/web/public/javascripts/map.js index 2547780..4b8b8f3 100644 --- a/web/public/javascripts/map.js +++ b/web/public/javascripts/map.js @@ -24,7 +24,7 @@ L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/toner-lite/{z}/{x}/{y}{r} ext: 'png' }).addTo(map); plotPointsOnMap(points); -if (!!window.EventSource) { +if (window.EventSource) { var source = new EventSource('/stream'); source.addEventListener('message', function(e) { @@ -46,4 +46,4 @@ if (!!window.EventSource) { }, false) } else { console.log("sse not supported."); -} \ No newline at end of file +} -- cgit v1.2.3