forked from deepgram-starters/node-live-transcription
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
101 lines (84 loc) · 2.89 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
const express = require("express");
const http = require("http");
const WebSocket = require("ws");
const { createClient, LiveTranscriptionEvents } = require("@deepgram/sdk");
const dotenv = require("dotenv");
dotenv.config();
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
const deepgramClient = createClient(process.env.DEEPGRAM_API_KEY);
let keepAlive;
const setupDeepgram = (ws) => {
const deepgram = deepgramClient.listen.live({
language: "en",
punctuate: true,
smart_format: true,
model: "nova",
});
if (keepAlive) clearInterval(keepAlive);
keepAlive = setInterval(() => {
console.log("deepgram: keepalive");
deepgram.keepAlive();
}, 10 * 1000);
deepgram.addListener(LiveTranscriptionEvents.Open, async () => {
console.log("deepgram: connected");
deepgram.addListener(LiveTranscriptionEvents.Transcript, (data) => {
console.log("deepgram: transcript received");
console.log("ws: transcript sent to client");
ws.send(JSON.stringify(data));
});
deepgram.addListener(LiveTranscriptionEvents.Close, async () => {
console.log("deepgram: disconnected");
clearInterval(keepAlive);
deepgram.finish();
});
deepgram.addListener(LiveTranscriptionEvents.Error, async (error) => {
console.log("deepgram: error received");
console.error(error);
});
deepgram.addListener(LiveTranscriptionEvents.Warning, async (warning) => {
console.log("deepgram: warning received");
console.warn(warning);
});
deepgram.addListener(LiveTranscriptionEvents.Metadata, (data) => {
console.log("deepgram: metadata received");
console.log("ws: metadata sent to client");
ws.send(JSON.stringify({ metadata: data }));
});
});
return deepgram;
};
wss.on("connection", (ws) => {
console.log("ws: client connected");
let deepgram = setupDeepgram(ws);
ws.on("message", (message) => {
console.log("ws: client data received");
if (deepgram.getReadyState() === 1 /* OPEN */) {
console.log("ws: data sent to deepgram");
deepgram.send(message);
} else if (deepgram.getReadyState() >= 2 /* 2 = CLOSING, 3 = CLOSED */) {
console.log("ws: data couldn't be sent to deepgram");
console.log("ws: retrying connection to deepgram");
/* Attempt to reopen the Deepgram connection */
deepgram.finish();
deepgram.removeAllListeners();
deepgram = setupDeepgram(ws);
} else {
console.log("ws: data couldn't be sent to deepgram");
}
});
ws.on("close", () => {
console.log("ws: client disconnected");
deepgram.finish();
deepgram.removeAllListeners();
deepgram = null;
});
});
app.use(express.static("public/"));
app.get("/", (req, res) => {
res.sendFile(__dirname + "/public/index.html");
});
server.listen(3000, () => {
console.log("Server is listening on port 3000");
});