-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
175 lines (144 loc) · 3.85 KB
/
server.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'use strict';
// standard express app plumbing
const express = require('express');
const app = express();
const morgan = require('morgan');
const path = require('path');
app.use(morgan('dev'));
app.use(express.static('dist'));
app.get('/', (req, res) => {
let template = 'index.html';
if (process.env.NODE_ENV === 'production') {
template = 'index.prod.html';
}
res.sendFile(path.join(__dirname, 'static', template));
});
const server = app.listen(3000, () => {
console.log(`Web server listening at http://localhost:${server.address().port}`);
});
// add socket.io server
const io = require('socket.io')(server);
// connect to the database
// rethinkdbdash provides transparent connection pooling
const r = require('rethinkdbdash')({
db: process.env.DATABASE_NAME || 'realtime_rethinkdb',
servers: [{
host: process.env.DATABASE_HOST || 'localhost',
port: parseInt(process.env.DATABASE_PORT) || 28015
}]
});
// * DATABASE SETUP
// * visit data explorer at http://localhost:8080/#dataexplorer
// r.dbCreate('realtime_rethinkdb')
// r.db('realtime_rethinkdb').tableCreate('teams')
// r.db('realtime_rethinkdb').table('teams').indexCreate('clicks')
// r.db('realtime_rethinkdb').table('teams').indexCreate('name')
// * RETHINKDB QUERIES
// SELECT *
// FROM teams
// ORDER BY clicks DESC
// LIMIT 10;
function fetchLeaderboard() {
return r.table('teams')
.orderBy({ index: r.desc('clicks')})
.limit(10);
};
// UPDATE teams
// SET updated_at = now(),
// clicks = clicks + 1
// WHERE name = "Awesome Team";
function incrementClicksForTeam(name) {
return r.table('teams')
.filter({ name })
.update({
updatedAt: r.now(),
clicks: r.row('clicks').add(1)
})
.run();
};
// SELECT *
// FROM teams
// WHERE name = "Awesome Team"
// LIMIT 1
const findTeamByName = (name) => {
return r.table('teams')
.filter({ name })
.limit(1)
.nth(0)
.run();
};
// INSERT INTO teams(name, clicks)
// VALUES ("Awesome Team", 0)
function createTeamWithName(name) {
return r.table('teams')
.insert({ name, clicks: 0 }, { returnChanges: true })
.run().then((result) => {
return result.changes[0]['new_val'];
});
};
function findOrCreateTeam(name) {
return findTeamByName(name).catch((error) => {
return createTeamWithName(name)
});
}
// * CHANGEFEEDS
function watchLeaderboard(callback) {
fetchLeaderboard()
.changes()
.map(r.row('new_val'))
.run((err, cursor) => {
if (err) {
callback(err);
return;
}
cursor.each(callback);
});
};
function watchTeam(team, callback) {
let stopWatching;
r.table('teams')
.get(team.id)
.changes()
.map(r.row('new_val'))
.run((err, cursor) => {
if (err) {
callback(err);
return;
}
stopWatching = () => cursor.close();
cursor.each(callback);
});
return () => { stopWatching && stopWatching(); };
}
watchLeaderboard((err, changes) => {
if (err) {
console.error(err);
} else {
io.emit('teamUpdated', changes);
}
});
io.on('connection', (socket) => {
// send the current leaderboard on connection
fetchLeaderboard().run().then((results) => {
socket.emit('leaderboard', results);
});
let stopWatching = null;
// User hit the 'Join Team' button
socket.on('join', (data) => {
findOrCreateTeam(data.name).then((team) => {
// send initial state
socket.emit('teamUpdated', team);
// send any changes for *that* user
// regardless of position on the leaderboard
stopWatching = watchTeam(team, (err, team) => {
socket.emit('teamUpdated', team);
});
});
});
// lets not keep these changefeeds ticking over if the user has left
socket.on('logout', () => { stopWatching && stopWatching(); });
socket.on('disconnect', () => { stopWatching && stopWatching(); });
socket.on('click', (data) => {
incrementClicksForTeam(data.name);
});
});