Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/real-time-client-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@speechmatics/real-time-client-react",
"version": "3.1.0",
"version": "3.2.0",
"description": "React hooks for interacting with the Speechmatics Real-Time API",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/real-time-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@speechmatics/real-time-client",
"version": "8.1.0",
"version": "8.2.0",
"description": "Client for the Speechmatics real-time API",
"main": "dist/index.js",
"browser": "dist/index.browser.js",
Expand Down
25 changes: 14 additions & 11 deletions packages/real-time-client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ export interface RealtimeClientOptions {
* Only set this if you're sure you need it.
*/
enableLegacy?: boolean;
/**
* Optionally specify the timeout (in milliseconds) to wait before throwing an error on starting and stopping
* For example, a value of 10_000 will throw an error if it takes more than 10 seconds to receive acknowledgement from the server after calling `start()` or `stopRecognition()`
* Default value is 10_000 (10 seconds)
*/
connectionTimeout?: number;
}
export type RealtimeTranscriptionConfig = Omit<
StartRecognition,
Expand All @@ -58,17 +64,19 @@ export type RealtimeTranscriptionConfig = Omit<
Partial<Pick<StartRecognition, 'audio_format'>>;

export class RealtimeClient extends TypedEventTarget<RealtimeClientEventMap> {
readonly url: string;
private readonly appId?: string;
private readonly enableLegacy: boolean;
timeout: number;

constructor(config: RealtimeClientOptions = {}) {
super();
this.url = config.url ?? 'wss://eu2.rt.speechmatics.com/v2';
this.appId = config.appId;
this.enableLegacy = config.enableLegacy ?? false;
this.timeout = config.connectionTimeout ?? 10_000;
}

readonly url: string;
private readonly appId?: string;
private readonly enableLegacy: boolean;

private socket?: WebSocket;

get socketState() {
Expand Down Expand Up @@ -229,10 +237,7 @@ export class RealtimeClient extends TypedEventTarget<RealtimeClientEventMap> {

return Promise.race([
waitForRecognitionStarted,
rejectAfter<RecognitionStarted>(
RT_CLIENT_RESPONSE_TIMEOUT_MS,
'RecognitionStarted',
),
rejectAfter<RecognitionStarted>(this.timeout, 'RecognitionStarted'),
]);
}

Expand All @@ -258,7 +263,7 @@ export class RealtimeClient extends TypedEventTarget<RealtimeClientEventMap> {

return Promise.race([
waitForEndOfTranscript,
rejectAfter(RT_CLIENT_RESPONSE_TIMEOUT_MS, 'EndOfTranscript'),
rejectAfter(this.timeout, 'EndOfTranscript'),
]);
}

Expand Down Expand Up @@ -289,8 +294,6 @@ const defaultAudioFormat = {
type: 'file',
} as const;

const RT_CLIENT_RESPONSE_TIMEOUT_MS = 10_000;

export class SpeechmaticsRealtimeError extends Error {
constructor(message: string, options?: ErrorOptions) {
super(message, options);
Expand Down