This repository has been archived by the owner on Jun 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
140 lines (107 loc) · 3.1 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
const querystring = require('querystring');
const Redis = require('ioredis');
const socketIo = require('socket.io');
const dogNames = require('dog-names');
const axios = require('axios');
const config = require('./config');
const pub = require('./lib/pub');
const sub = require('./lib/sub');
const redis = require('./lib/redis');
const slackHandler = require('./lib/slackHandler');
const messagesChannel = 'chat-messages';
const chatHistory = 'chat-history';
const messagesCounter = 'chat-counter:messages';
const domainsSet = 'chat-domains';
const chattersCounter = 'chat-chatters';
const pageViewsCounter = 'chat-views';
const liveChattersCounter = 'chat-live';
slackHandler.listen(3055, '0.0.0.0');
const io = socketIo(3050);
const domains = {};
const stats = new Set();
async function getStats() {
return {
messages: await redis.get(messagesCounter) || 0,
domains: await redis.scard(domainsSet) || 0,
chatters: await redis.get(chattersCounter) || 0,
liveChatters: await redis.get(liveChattersCounter) || 0,
pageViews: await redis.get(pageViewsCounter) || 0
};
}
setInterval(async () => {
const data = await getStats();
for (const socket of stats) {
socket.emit('stats', data);
}
}, 1000);
io.on('connection', socket => {
socket.on('subscribe-stats', async () => {
socket.emit('stats', await getStats());
stats.add(socket);
});
socket.on('disconnect', () => {
stats.delete(socket);
});
socket.on('init', async (domain, isNewVisitor) => {
await redis.incr(pageViewsCounter);
await redis.incr(liveChattersCounter);
await redis.sadd(domainsSet, domain);
if (isNewVisitor === true) {
await redis.incr(chattersCounter);
}
if (!(domains[domain] instanceof Set)) {
domains[domain] = new Set();
}
domains[domain].add(socket);
let name = dogNames.allRandom();
socket.emit('name', name);
socket.on('message', async text => {
if (text.startsWith('/')) {
const commandHandlers = {
name(_name = '') {
name = _name.trim().slice(0, 9);
if (name.length < 2) {
return;
}
socket.emit('name', name);
}
};
const [command, ...args] = text.slice(1).split(' ');
if (command in commandHandlers) {
commandHandlers[command](...args);
}
return;
}
const message = {
name,
text
};
await pub.publish(messagesChannel, JSON.stringify({
message,
domain
}));
await redis.lpush(`${chatHistory}:${domain}`, JSON.stringify(message));
await redis.incr(messagesCounter);
await axios.post(process.env.WEBHOOK, {
text: `${name}: ${text}`
});
});
socket.on('disconnect', async () => {
domains[domain].delete(socket);
await redis.decr(liveChattersCounter);
});
const rawMessages = await redis.lrange(`${chatHistory}:${domain}`, 0, -1);
rawMessages.reverse();
for (const rawMessage of rawMessages) {
socket.emit('message', JSON.parse(rawMessage));
}
});
});
sub.on('message', (channel, rawMessage) => {
const {message, domain} = JSON.parse(rawMessage);
if (domains[domain] instanceof Set) {
for (const socket of domains[domain]) {
socket.emit('message', message);
}
}
});