Hello @jcolp (I didn’t want to disturb you but I desperately need some help to get my code working as my deadline is about to hit). I read the other posts about externalmedia and your answers but I still can’t figure out what is wrong with my code.
I’m using Asterisk 23.1.0 for an AI agent.
What I’m trying to do is connecting ari and starting an externalmedia websocket connection for audio receiving. I’m planning to handle calls with ari and process the audio through a realtime openai session.
ari.ts
import ari, { Bridge, Client } from 'ari-client';
import "./otel"
const ASTERISK_HOST = 'localhost';
const ASTERISK_PORT = 8088;
const ASTERISK_USER = '......';
const ASTERISK_PASS = '.......';
const APP_NAME = 'audio-app';
import net from 'net';
// --- AYARLAR ---
const ASTERISK_URL = 'http://localhost:8088/ari';
const MEDIA_SERVER_PORT = 4096;
const MEDIA_SERVER_IP = '127.0.0.1';
const mediaServer = net.createServer((socket) => {
console.log('Asterisk medya sunucusuna bağlandı!');
socket.on('data', (data) => {
console.log(data.toString())
});
socket.on('end', () => {
console.log('Medya akışı bitti.');
});
});
mediaServer.listen(MEDIA_SERVER_PORT, () => {
console.log(`TCP Media Server ${MEDIA_SERVER_PORT} portunda dinliyor...`);
});
ari.connect(ASTERISK_URL, ASTERISK_USER, ASTERISK_PASS)
.then((ari) => {
ari.start(APP_NAME);
console.log('ARI Başlatıldı, çağrı bekleniyor...');
ari.on('StasisStart', async (event, incomingChannel) => {
console.log(`Çağrı geldi: ${incomingChannel.caller.number}. Analiz yapılıyor...`);
await incomingChannel.answer();
const bridge = await ari.bridges.create({ type: 'mixing' });
await bridge.addChannel({ channel: incomingChannel.id });
const externalMediaChannel = await ari.channels.externalMedia({
app: APP_NAME,
external_host: `${MEDIA_SERVER_IP}:${MEDIA_SERVER_PORT}`,
format: 'slin16',
encapsulation: 'audiosocket',
transport: 'tcp',
connection_type: 'client',
direction: 'both'
});
await bridge.addChannel({ channel: externalMediaChannel.id });
console.log('Sistem hazır! Ses akıyor, ARI kontrolü bizde.');
incomingChannel.on('StasisEnd', async () => {
console.log('Çağrı bitti, temizlik yapılıyor.');
try {
await bridge.destroy();
await externalMediaChannel.hangup();
} catch {
}
});
});
})
.catch((err) => console.error(err));
extensions.conf
[general]
static = yes
writeprotect = no
[friendzone]
exten => 42,1,NoOp(CAGRI ALINDI)
same => n,Set(UUID=${UUID()})
same => n,NoOp(CAGRI UUID: ${UUID})
same => n,Stasis(audio-app,${UUID})
same => n,Hangup()
Example error logs
Connected to Asterisk 23.1.0 currently running on debian (pid = 641374)
Activating Stasis app 'audio-app'
-- Executing [42@friendzone:1] NoOp("PJSIP/2-00000023", "CAGRI ALINDI") in new stack
-- Executing [42@friendzone:2] Set("PJSIP/2-00000023", "UUID=e7d03d9d-497f-420a-a715-df83340a5440") in new stack
-- Executing [42@friendzone:3] NoOp("PJSIP/2-00000023", "CAGRI UUID: e7d03d9d-497f-420a-a715-df83340a5440") in new stack
-- Executing [42@friendzone:4] Stasis("PJSIP/2-00000023", "audio-app,e7d03d9d-497f-420a-a715-df83340a5440") in new stack
-- Channel PJSIP/2-00000023 joined 'simple_bridge' stasis-bridge <769dc693-f807-4ce9-afc7-c8f2a015592e>
[2026-02-07 09:28:06] WARNING[648672]: res_http_websocket.c:575 ws_safe_read: Web socket closed abruptly
[2026-02-07 09:28:06] WARNING[648672]: ari/ari_websockets.c:228 session_read: WebSocket read error: Success
Deactivating Stasis app 'audio-app'
And nodejs logs
node:internal/process/promises:391
triggerUncaughtException(err, true /* fromPromise */);
^
Error: {
"message": "An internal error prevented this request from being handled"
}
at SwaggerRequest.swaggerError [as errorCallback] (C:\Users\Luz\Desktop\asterisk\asterisk.js\node_modules\ari-client\lib\client.js:253:23)
at Object.error (C:\Users\Luz\Desktop\asterisk\asterisk.js\node_modules\swagger-client\lib\swagger.js:1077:24)
at EventEmitter.error (C:\Users\Luz\Desktop\asterisk\asterisk.js\node_modules\swagger-client\lib\swagger.js:1296:19)
at EventEmitter.emit (node:events:519:28)
at emit (C:\Users\Luz\Desktop\asterisk\asterisk.js\node_modules\shred\lib\shred\request.js:454:21)
at C:\Users\Luz\Desktop\asterisk\asterisk.js\node_modules\shred\lib\shred\request.js:473:9
at setBodyAndFinish (C:\Users\Luz\Desktop\asterisk\asterisk.js\node_modules\shred\lib\shred\response.js:103:7)
at IncomingMessage.<anonymous> (C:\Users\Luz\Desktop\asterisk\asterisk.js\node_modules\shred\lib\shred\response.js:120:7)
This error messages showed up while calling for externalmedia on nodejs.
Also the necessary modules are properly running.
debian*CLI> module show like chan_rtp
Module Description Use Count Status Support Level
chan_rtp.so RTP Media Channel 0 Running core
1 modules loaded
debian*CLI> module show like audiosocket
Module Description Use Count Status Support Level
app_audiosocket.so AudioSocket Application 0 Running extended
chan_audiosocket.so AudioSocket Channel 0 Running extended
res_audiosocket.so AudioSocket support 2 Running extended
3 modules loaded