.intro.md•2.87 kB
streams
=================
[](https://fonoster.com)
[](https://npmjs.org/package/@fonoster/streams)
[](https://npmjs.org/package/@fonoster/streams)
[](https://github.com/fonoster/fonoster/blob/main/package.json)
This is a NodeJS implementation of the AudioSocket protocol, which is a simple protocol for accessing bidirectional audio streams from Asterisk.
* [Installation](#installation)
* [Example](#example)
* [APIs](#apis)
## Installation
```sh-session
$ npm install --save @fonoster/streams
```
## Example
While the AudioSocket is a utility for Fonoster Streams, it can be used as a standalone module. To use this library with Asterisk, you must configure your dialplan to use the `AudioSocket` application. Here is an example of how to use the AudioSocket with Asterisk:
```
exten = 100,1,Verbose("Call to AudioSocket via Dialplan Application")
same = n,Answer()
same = n,AudioSocket(40325ec2-5efd-4bd3-805f-53576e581d13,server.example.com:9092)
same = n,Hangup()
```
Or with the `Dial` application:
```
exten = 100,1,Verbose("Call to AudioSocket via Dialplan Application")
same = n,Answer()
same = n,Dial(SIP/100,30,A(AudioSocket(40325ec2-5efd-4bd3-805f-53576e581d13,server.example.com:9092)))
same = n,Hangup()
```
Connecting to the AudioSocket server using ARI with the `externalMedia` endpoint is also possible. You must ensure you set the transport to `TCP` the UUID in the data field.
> Currently, the payload towards Asterisk is limited to signed linear, 16-bit, 8kHz, mono PCM (little-endian). However, the payload from Asterisk can be changed with the `format` parameter when using ARI. Please see the [AudioSocket Server](https://github.com/silentindark/audiosocket_server) implementation, for interesting notes about AudioSocket.
Once Asterisk is configured, you can use the `AudioSocket` class to create a server that listens for connections. Here is an example of how to use the `AudioSocket` class:
```js
const { AudioSocket } = require("@fonoster/streams");
const audioSocket = new AudioSocket();
audioSocket.onConnection(async (req, res) => {
console.log("new connection from:", req.ref);
res.on("data", (data) => {
// Do something with the audio data
);
res.on("end", () => {
// Do something when the stream ends
});
res.on("error", (err) => {
// Do something when an error occurs
});
// Utility for playing audio files
await res.play("/path/to/audio/file");
});
audioSocket.listen(9092, () => {
console.log("server listening on port 9092");
});
```
## APIs
* [`AudioSocket`](#AudioSocket)
* [`AudioStream`](#AudioStream)