Skip to content

Commit

Permalink
Merge pull request #1 from speechmatics/fix-node-example-end-of-events
Browse files Browse the repository at this point in the history
Fix EndOfTranscript event firing twice and update example
  • Loading branch information
nickgerig authored Sep 4, 2023
2 parents 6333e97 + cd9785b commit 4573233
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 29 deletions.
27 changes: 15 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,35 +109,38 @@ import { RealtimeSession } from 'speechmatics';

// imports helpful for the file streaming
const fs = require('fs');
const path = require('path');

// init the session
const session = new RealtimeSession(YOUR_API_KEY);

//add listeners
session.addListener('RecognitionStarted', () => {
console.log('session started');
console.log('RecognitionStarted');
});

session.addListener('Error', (error) => {
console.log('session error', error);
});

session.addListener('AddTranscript', (message) => {
console.log('message', message);
console.log('AddTranscript', message);
});

session.addListener('AddPartialTranscript', (message) => {
console.log('message', message);
console.log('AddPartialTranscript', message);
});

session.addListener('EndOfStream', () => {
console.log('Session stopped');
session.addListener('EndOfTranscript', () => {
console.log('EndOfTranscript');
});

//start session which is an async method
session.start().then(() => {
//prepare file stream
const fileStream = fs.createReadStream('examples/example_files/example.wav');
const fileStream = fs.createReadStream(
path.join(__dirname, 'example_files/example.wav'),
);

//send it
fileStream.on('data', (sample) => {
Expand Down Expand Up @@ -172,23 +175,23 @@ const session = new RealtimeSession(YOUR_JWT);

//add listeners
session.addListener('RecognitionStarted', () => {
console.log('session started');
console.log('RecognitionStarted');
});

session.addListener('Error', (error) => {
console.log('session error', error);
});

session.addListener('AddTranscript', (message) => {
console.log('message', message);
console.log('AddTranscript', message);
});

session.addListener('AddPartialTranscript', (message) => {
console.log('message', message);
console.log('AddPartialTranscript', message);
});

session.addListener('EndOfStream', () => {
console.log('Session stopped');
session.addListener('EndOfTranscript', () => {
console.log('EndOfTranscript');
});

//start session which is an async method
Expand Down Expand Up @@ -258,6 +261,6 @@ We'd love to see your contributions! Please read our [contributing guidelines](.

## Feedback & Help

- For feature requests or bugs [open an issue](https://github.com/speechmatics/speechmatics-js/issues/new)
- For feature requests or bugs [open an issue](https://github.com/speechmatics/speechmatics-js-sdk/issues/new)
- To provide direct feedback, email us at [devrel@speechmatics.com](mailto:devrel@speechmatics.com)
- We're [@speechmatics](https://twitter.com/Speechmatics) on Twitter too!
19 changes: 12 additions & 7 deletions examples/example_rt_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,30 @@ const path = require('path');
const { RealtimeSession } = require('../dist');

if (parseInt(process.version.match(/(?:v)([0-9]{2})/)[1]) < 18) {
throw new Error("Requires node 18 or higher. If this isn't possible, see our documentation about polyfilling");
throw new Error(
"Requires node 18 or higher. If this isn't possible, see our documentation about polyfilling",
);
}
const session = new RealtimeSession(process.env.API_KEY);

session.addListener('RecognitionStarted', () => {
console.log('session started');
console.log('RecognitionStarted');
});

session.addListener('Error', (error) => {
console.log('session error', error);
});

session.addListener('AddTranscript', (message) => {
console.log('transcript> ', message.metadata.transcript);
console.log('AddTranscript', message.metadata.transcript);
});

session.addListener('AddPartialTranscript', (message) => {
// console.log('partial', message);
// console.log('AddPartialTranscript', message);
});

session.addListener('EndOfStream', () => {
console.log('Session stopped');
session.addListener('EndOfTranscript', () => {
console.log('EndOfTranscript');
});

session
Expand All @@ -41,10 +43,13 @@ session
})
.then(() => {
//prepare file stream
const fileStream = fs.createReadStream(path.join(__dirname, 'example_files/example.wav'));
const fileStream = fs.createReadStream(
path.join(__dirname, 'example_files/example.wav'),
);

//send it
fileStream.on('data', (sample) => {
console.log('sending audio', sample.length);
session.sendAudio(sample);
});

Expand Down
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"name": "speechmatics",
"version": "3.0.1",
"version": "3.0.2",
"description": "Speechmatics Javascript Libraries",
"types": "./dist/index.d.ts",
"main": "./dist/index.js",
"files": ["dist"],
"files": [
"dist"
],
"scripts": {
"build:clean": "rm -rf dist",
"build:cjs": "./build.js",
Expand All @@ -15,7 +17,9 @@
"check": "npx rome check .",
"format": "npx rome format --write ."
},
"precommit": ["check"],
"precommit": [
"check"
],
"repository": {
"type": "git",
"url": "git+https://github.com/speechmatics/speechmatics-js-sdk.git"
Expand Down
4 changes: 2 additions & 2 deletions src/management-platform/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export default async function getShortLivedToken(
) {
if (typeof window !== 'undefined') {
console.warn(
"Requesting a short lived token from a browser is not recommended. \
More info at https://github.com/speechmatics/speechmatics-js#readme",
'Requesting a short lived token from a browser is not recommended. \
More info at https://github.com/speechmatics/speechmatics-js#readme',
);
}
const jsonResponse = await request<{ key_value: string }>(
Expand Down
3 changes: 0 additions & 3 deletions src/realtime/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ export class RealtimeSession {
onError: (data: ModelError) => {
this.emitter.emit(MessagesEnum.Error, data);
},
onDisconnect: () => {
this.emitter.emit(MessagesEnum.EndOfTranscript, this);
},
};
}

Expand Down
2 changes: 0 additions & 2 deletions src/realtime/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export class RealtimeSocketHandler {
): Promise<void> {
this.seqNoIn = 0;


return this.socketWrap.connect(runtimeURL, runtimeKey, appId);
}

Expand Down Expand Up @@ -162,7 +161,6 @@ export class RealtimeSocketHandler {
}
};


private onSocketDisconnect = () => {
this.sub.onDisconnect?.();
};
Expand Down

0 comments on commit 4573233

Please sign in to comment.