-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
88 lines (71 loc) · 2.41 KB
/
index.js
File metadata and controls
88 lines (71 loc) · 2.41 KB
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
79
80
81
82
83
84
85
86
87
88
const express = require('express');
const mustacheExpress = require('mustache-express');
const fs = require('fs');
const httpServer = require('http').Server;
const socketio = require('socket.io');
const ss = require('socket.io-stream');
const mergeStream = require('merge-stream');
const { MongoClient } = require('mongodb');
const WikiStream = require('./streams/wikipedia');
const GPlacesStream = require('./streams/gplaces');
const app = express();
const server = httpServer(app);
const io = socketio(server);
const HOST = process.env.NODE_HOST || 'localhost';
const PORT = process.env.NODE_PORT || 3000;
const DBHOST = process.env.DB_HOST || 'localhost';
const DBPORT = process.env.DB_PORT || 27017;
const DB = 'pois';
const mongoURL = `mongodb://${DBHOST}:${DBPORT}/${DB}`;
MongoClient.connect(mongoURL, (err, db) => {
if (err) {
console.error('db error:', error);
return;
}
console.log('connected to db');
const collection = db.collection('pois');
io.of('/geosearch').on('connection', (socket) => {
console.log('new client connected');
socket.on('geosearch', (y, x, r) => {
const clientstream = ss.createStream();
ss(socket).emit('poi', clientstream, {});
// api streams to db
mergeStream(
new WikiStream(y, x, r),
new GPlacesStream(y, x, r),
)
.on('data', (chunk) => {
const poi = JSON.parse(chunk.toString());
collection.update(poi, poi, { upsert: true })
.then((r) => {
console.log('result', r.result);
})
.catch((err) => { console.error(err); });
})
.on('end', () => {
console.log('stream ended');
});
// stream from db to client
collection.find({ 'location' : {
$near : {
$geometry: { type: 'Point', coordinates: [x, y] },
$maxDistance: r,
}}
})
.stream({ transform: doc => JSON.stringify(doc)})
.pipe(clientstream);
});
socket.on('disconnect', () => {
console.log('client disconnected');
});
});
app
.engine('html', mustacheExpress())
.set('view engine', 'mustache')
.set('views', `${__dirname}/views`)
.use(express.static(`${__dirname}/public`))
.get('/', (req, res) => {
res.render('index.html', { res: () => (1+1) });
});
server.listen(PORT, HOST, () => { console.log(`listening on port ${PORT}`); });
});