npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ipcom/asterisk-ari

v0.0.144

Published

JavaScript client for Asterisk REST Interface.

Downloads

9,764

Readme

@ipcom/asterisk-ari

A modern JavaScript/TypeScript library for interacting with the Asterisk REST Interface (ARI).

Features

  • Complete Asterisk ARI support
  • Written in TypeScript with full type support
  • WebSocket support for real-time events
  • Automatic reconnection management
  • Simplified channel and playback handling
  • ESM and CommonJS support
  • Complete type documentation

Installation

npm install @ipcom/asterisk-ari

Basic Usage

Initial Setup

import { AriClient } from '@ipcom/asterisk-ari';

const client = new AriClient({
  host: 'localhost',      // Asterisk host
  port: 8088,            // ARI port
  username: 'username',   // ARI username
  password: 'password',   // ARI password
  secure: false          // Use true for HTTPS/WSS
});

WebSocket Connection

// Connect to WebSocket to receive events
await client.connectWebSocket(['myApp']); // 'myApp' is your application name

// Listen for specific events
client.on('StasisStart', event => {
  console.log('New channel started:', event.channel.id);
});

client.on('StasisEnd', event => {
  console.log('Channel ended:', event.channel.id);
});

// Listen for DTMF events
client.on('ChannelDtmfReceived', event => {
  console.log('DTMF received:', event.digit);
});

// Close WebSocket connection
client.closeWebSocket();

Event Instances

Channel and Playback Instances in Events

When working with WebSocket events, you get access to both the raw event data and convenient instance objects that allow direct interaction with the channel or playback:

client.on('StasisStart', async event => {
  // event.channel contains the raw channel data
  console.log('New channel started:', event.channel.id);

  // event.instanceChannel provides a ready-to-use ChannelInstance
  const channelInstance = event.instanceChannel;
  
  // You can directly interact with the channel through the instance
  await channelInstance.answer();
  await channelInstance.play({ media: 'sound:welcome' });
});

// Similarly for playback events
client.on('PlaybackStarted', async event => {
  // event.playback contains the raw playback data
  console.log('Playback ID:', event.playback.id);

  // event.instancePlayback provides a ready-to-use PlaybackInstance
  const playbackInstance = event.instancePlayback;
  
  // Direct control through the instance
  await playbackInstance.control('pause');
});

This approach provides two key benefits:

  1. No need to manually create instances using client.Channel() or client.Playback()
  2. Direct access to control methods without additional setup

Comparing Approaches

Traditional approach:

client.on('StasisStart', async event => {
  // Need to create channel instance manually
  const channel = client.Channel(event.channel.id);
  await channel.answer();
});

Using instance from event:

client.on('StasisStart', async event => {
  // Instance is already available
  await event.instanceChannel.answer();
});

This feature is particularly useful when handling multiple events and needing to perform actions on channels or playbacks immediately within event handlers.

Channel Handling

// Create a channel instance
const channel = client.Channel();

// Originate a call
await channel.originate({
  endpoint: 'PJSIP/1000',
  extension: '1001',
  context: 'default',
  priority: 1
});

// Answer a call
await channel.answer();

// Play audio
const playback = await channel.play({
  media: 'sound:welcome'
});

// Hangup the channel
await channel.hangup();

Playback Handling

// Create a playback instance
const playback = client.Playback();

// Monitor playback events
playback.on('PlaybackStarted', event => {
  console.log('Playback started:', event.playback.id);
});

playback.on('PlaybackFinished', event => {
  console.log('Playback finished:', event.playback.id);
});

// Control playback
await playback.control('pause');  // Pause
await playback.control('unpause'); // Resume
await playback.control('restart'); // Restart
await playback.stop();            // Stop

Specific Channel Monitoring

// Create an instance for a specific channel
const channel = client.Channel('channel-id');

// Monitor specific channel events
channel.on('ChannelStateChange', event => {
  console.log('Channel state changed:', event.channel.state);
});

channel.on('ChannelDtmfReceived', event => {
  console.log('DTMF received on channel:', event.digit);
});

// Get channel details
const details = await channel.getDetails();
console.log('Channel details:', details);

// Handle channel variables
await channel.getVariable('CALLERID');
await channel.setVariable('CUSTOM_VAR', 'value');

Channel Playback Handling

// Play audio on a specific channel
const channel = client.Channel('channel-id');
const playback = await channel.play({
  media: 'sound:welcome',
  lang: 'en'
});

// Monitor specific playback
playback.on('PlaybackStarted', event => {
  console.log('Playback started on channel');
});

// Control playback
await channel.stopPlayback(playback.id);
await channel.pausePlayback(playback.id);
await channel.resumePlayback(playback.id);

Error Handling

try {
  await client.connectWebSocket(['myApp']);
} catch (error) {
  console.error('Error connecting to WebSocket:', error);
}

// Using with async/await
try {
  const channel = client.Channel();
  await channel.originate({
    endpoint: 'PJSIP/1000'
  });
} catch (error) {
  console.error('Error originating call:', error);
}

TypeScript Support

The library provides complete type definitions for all operations:

import type { 
  Channel, 
  ChannelEvent, 
  WebSocketEvent 
} from '@ipcom/asterisk-ari';

// Types will be available for use
const handleChannelEvent = (event: ChannelEvent) => {
  const channelId: string = event.channel.id;
};

Additional Features

The library provides access to many other ARI features:

  • Bridge management
  • Endpoint handling
  • Sound manipulation
  • Application control
  • Asterisk system information
  • And much more...

Advanced Examples

Bridge Creation and Channel Management

// Create and manage a bridge
const bridge = await client.bridges.createBridge({
  type: 'mixing',
  name: 'myBridge'
});

// Add channels to bridge
await client.bridges.addChannels(bridge.id, {
  channel: ['channel-id-1', 'channel-id-2']
});

Recording Management

// Start recording on a channel
const channel = client.Channel('channel-id');
await channel.record({
  name: 'recording-name',
  format: 'wav',
  maxDurationSeconds: 60,
  beep: true
});

External Media

// Create external media channel
const channel = await client.channels.createExternalMedia({
  app: 'myApp',
  external_host: 'media-server:8088',
  format: 'slin16'
});

API Reference

For complete API documentation, please refer to the TypeScript types and interfaces exported by the package.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

Apache-2.0