Skip to content

Commit

Permalink
fix: find a fix to allow the client to work in either websocket envir…
Browse files Browse the repository at this point in the history
…onment (#199)
  • Loading branch information
lukeocodes authored Nov 15, 2023
1 parent c0def14 commit 0dce6e5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"cross-fetch": "^3.1.5",
"deepmerge": "^4.3.1",
"events": "^3.3.0",
"modern-isomorphic-ws": "^1.0.5"
"isomorphic-ws": "^5.0.0",
"ws": "^8.14.2"
},
"devDependencies": {
"@commitlint/cli": "^17.6.7",
Expand All @@ -81,8 +82,7 @@
"typedoc": "^0.22.16",
"typescript": "^4.5.5",
"webpack": "^5.69.1",
"webpack-cli": "^4.9.2",
"ws": "^8.13.0"
"webpack-cli": "^4.9.2"
},
"husky": {
"hooks": {
Expand Down
12 changes: 8 additions & 4 deletions src/packages/LiveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { appendSearchParams, isBrowser } from "../lib/helpers";
import { DeepgramError } from "../lib/errors";
import { DEFAULT_OPTIONS } from "../lib/constants";
import { LiveConnectionState, LiveTranscriptionEvents } from "../lib/enums";
import WebSocket from "modern-isomorphic-ws";
import WebSocket from "isomorphic-ws";

import type {
LiveSchema,
Expand All @@ -28,15 +28,19 @@ export class LiveClient extends AbstractWsClient {
url.protocol = url.protocol.toLowerCase().replace(/(http)(s)?/gi, "ws$2");
appendSearchParams(url.searchParams, this.transcriptionOptions);

if (isBrowser()) {
this._socket = new WebSocket(url.toString(), ["token", this.key]);
} else {
try {
this._socket = new WebSocket(url.toString(), {
headers: {
Authorization: `token ${this.key}`,
...this.options?.global?.headers,
},
});
} catch (e) {
if (e instanceof SyntaxError) {
this._socket = new WebSocket(url.toString(), ["token", this.key]);
} else {
throw e; // let others bubble up
}
}

this._socket.onopen = () => {
Expand Down

0 comments on commit 0dce6e5

Please sign in to comment.