-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
51 lines (48 loc) · 1.46 KB
/
index.js
File metadata and controls
51 lines (48 loc) · 1.46 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
const PubNub = require('pubnub');
const r = require('rethinkdb');
const pubnub = new PubNub({
publishKey : 'pub-c-53eaf64c-9df8-4ca7-9116-9ecb0d61feef',
subscribeKey : 'sub-c-b507301c-216b-11e6-8b91-02ee2ddab7fe',
});
pubnub.subscribe({
channels : ['carobserver:position'],
});
r.connect({ host : 'localhost', port : 28015, db : 'carobserver' }).then((conn) => {
r.table('location').changes().run(conn).then((cursor) => {
cursor.on('data', (message) => {
console.log(`sending car position ${Math.random()}`);
pubnub.publish({
channel : 'carobserver:current-car-position',
message : message.new_val,
});
});
}).catch((err) => {
//eslint-disable-next-line
console.log(err);
});
});
pubnub.addListener({
message : (messageData) => {
console.log(`receiving car position ${Math.random()}`);
const position = messageData.message;
r.connect({ host : 'localhost', port : 28015, db : 'carobserver' }).then((conn) => {
r.table('location').insert({
latitude : position[0],
longitude : position[1],
speed : position[2],
accuracy : position[3],
provider : position[4],
date : new Date(),
}).run(conn).then(() => {
conn.close();
}).catch((err) => {
//eslint-disable-next-line
console.log(err);
conn.close();
});
}).catch((err) => {
//eslint-disable-next-line
console.log(err);
});
},
});