-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (38 loc) · 1.1 KB
/
index.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
import express from "express";
import http from "http";
import { Server } from "socket.io";
import "dotenv/config";
import main from "./api.js";
import { error } from "console";
const app = express();
const server = http.createServer(app);
const io = new Server(server);
app.use(express.static("public"));
io.on("connection", (client) => {
console.log("user connected");
// receiving msg
client.on("message", (msg) => {
// logging the message
console.log("message received: " + msg);
// calling the langflow api call
(async () => {
try {
const result = await main(msg);
client.emit("message", { status: "ok", res: result, error: "" });
} catch (error) {
client.emit("message", {
status: "fail",
res: "",
error: `Error occured in socket: ${error.message}`,
});
}
})();
// reply the answer given by langflow.
});
client.on("disconnect", () => {
console.log("user disconnected");
});
});
server.listen(process.env.port, () =>
console.log(`server is listeing on http://localhost:${process.env.port}`)
);