-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
224 lines (186 loc) · 6.11 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
const express = require("express");
const app = express();
const http = require("http").Server(app);
var cors = require("cors");
var FormData = require('form-data');
app.use(cors());
const io = require("socket.io")(http, {
cors: {
// Avoid allowing all origins.
origin: "*",
// methods: ["GET", "POST"],
}, maxHttpBufferSize: 1e8,
});
const port = process.env.PORT || 3001;
const redisWorkerEnabled = process.env.ENABLE_REDIS_WORKER || 'false';
const celeryWorkerEnabled = process.env.ENABLE_CELERY_WORKER || 'false';
const bentoWorkerEnabled = process.env.ENABLE_BENTOML_WORKER || 'true';
const REDIS_URL = process.env.REDIS_URL || 'redis://myredis-headless';
const CELERY_BROKER_URL = process.env.CELERY_BROKER_URL || 'amqp://admin:mypass@rabbitmq-service:5672';
const CELERY_RESULT_BACKEND = process.env.CELERY_RESULT_BACKEND || 'redis://myredis-headless:6379/0';
const YATAI_DEPLOYMENT_URL = process.env.YATAI_DEPLOYMENT_URL || 'http://0.0.0.0:3000/predict_async';
const redis = require("redis");
app.use(express.static(__dirname + "/public"));
app.get("/", (req, res) => {
res.sendFile("/index.html");
});
const celery = require('celery-node');
if (celeryWorkerEnabled === 'true') {
var celery_app = celery.createClient(
CELERY_BROKER_URL,
CELERY_RESULT_BACKEND
);
}
// Set up patient and physicist namespaces.
const patients = io.of("/patients");
const physicians = io.of("/physicians");
// Initialize redis client.
if (redisWorkerEnabled === 'true') {
var client = redis.createClient(
{
url: REDIS_URL
}
);
client.on("error", (err) => console.log("Redis client Error", err));
(async () => {
await client.connect();
streamConsumer();
})();
}
// consume new elements of output emotion stream
async function streamConsumer() {
let currentId = "$"; // Use as last ID the maximum ID already stored in the stream
let patientId;
while (true) {
try {
let response = await client.xRead(
redis.commandOptions({
isolated: true,
}),
[
// XREAD can read from multiple streams, starting at a
// different ID for each...
{
key: "main:results",
id: currentId,
},
],
{
// Read 1 entry at a time, block for 5 seconds if there are none.
COUNT: 1,
BLOCK: 50000,
}
);
if (response) {
patientId = response[0].messages[0].message.userId;
console.log('Received Redis Worker Result:')
console.log(response[0].messages[0].message);
physicians
.to(patientId)
.volatile.emit("emotion", response[0].messages[0].message.emotions);
// Get the ID of the first (only) entry returned.
currentId = response[0].messages[0].id;
}
} catch (err) {
console.error(err);
}
}
}
// Handle patient connections.
patients.on("connection", (socket) => {
console.log("patient connected");
// Add image to redis' input stream and/or celery worker.
socket.on("image", (msg) => {
if (typeof msg.img === 'undefined') {
console.log('Warning: Image event got triggered without an image attached.')
return;
}
console.info(`Image of size ${msg.img.byteLength} received.`);
if (redisWorkerEnabled === 'true') {
client.xAdd("main", "*", msg, "MAXLEN", "~", "1000");
}
if (celeryWorkerEnabled === 'true') {
request = { "usr": msg.userId, "image": msg.img }
celery_app.sendTask("tasks.EmotionRecognition", undefined, { 'request': request })
}
if (bentoWorkerEnabled === 'true') {
const formData = new FormData()
const annotations = { userId: msg.userId, conferenceId: msg.conferenceId }
formData.append('annotations', JSON.stringify(annotations))
formData.append('image', msg.img, 'patient.png')
formData.submit(YATAI_DEPLOYMENT_URL, function (err, res) {
if (err) {
console.log(err);
}
else {
const body = []
res.on('data',
(chunk) => body.push(chunk)
);
res.on('end', () => {
try {
const resString = Buffer.concat(body).toString()
const reply = JSON.parse(resString)
console.log('Received BentoML Worker Result:')
console.log(reply)
console.log(reply.output.length)
if (reply.output.length > 0) {
physicians
.to(reply.emotions.userId)
.volatile.emit("emotion", JSON.stringify(reply.emotions));
}
}
catch (error) {
console.log('BentoML Worker Error:')
console.error(error);
}
})
}
});
}
});
socket.on("disconnect", (reason) => {
console.log(`patient disconnected. Reason: ${reason}`);
});
});
// Handle physicians connections
physicians.on("connection", function (socket) {
console.log("physician connected");
socket.on("subscribe", function (patientId) {
socket.join(patientId);
});
socket.on("unsubscribe", function (patientId) {
socket.leave(patientId);
});
socket.on("disconnect", (reason) => {
console.log("physician disconnected");
});
});
// Handle all connections. For testing purposes.
io.on("connection", (socket) => {
console.log("main namespace connected");
// Handle incoming messages from celery worker.
socket.on("send_result_to_server", (msg) => {
for (const patientResult of msg.data) {
console.log('Received Celery Worker Result:')
console.log(patientResult);
const patientId = patientResult.userId;
physicians
.to(patientId)
.volatile.emit("emotion", patientResult.emotions);
}
});
socket.on("chat message", (msg) => {
// io.emit("chat message", msg);
io.to("test").emit("chat message", msg);
});
socket.on("disconnect", (reason) => {
console.log("main namespace disconnected");
});
socket.on("subscribe", function (patientId) {
socket.join(patientId);
});
});
http.listen(port, () => {
console.log(`Socket.IO server running at http://localhost:${port}/`);
});