You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Graham Brown edited this page Jan 1, 2022
·
9 revisions
Understanding ARI
Before continuing, we need to understand a little about ARI 🤔
ARI is an asynchronous API that allows developers to build communications applications by exposing the raw primitive objects in Asterisk - channels, bridges, endpoints, media, etc. - through an intuitive REST interface. The state of the objects being controlled by the user are conveyed via JSON events over a WebSocket.
( From the Asterisk Wiki )
ARI is quite a low level API, which means the application you build with ARI needs to take on a lot of responsibilities for those raw primitive objects. Your application should be reacting to state changes on those objects, there are 3 ways to get the state changes:
First off we'll need to setup Asterisk, then we can get into the Java code 😀
(:spiral_notepad: the Examples has a vagrant box which does the setup automatically 😎)
Asterisk Setup
Edit /etc/asterisk/ari.conf and create a user ari4java that we will use for connecting.
[general]
enabled = yes
[ari4java]
type = user
read_only = no
password = yothere
password_format = plain
Edit /etc/asterisk/http.conf and turn on the embedded HTTP server.
Assuming 192.168.56.44 is the IP of your Asterisk server. Using AriVersion.IM_FEELING_LUCKY is not recommended as we try determine the version by connecting to Asterisk. It is better to use a specific version like AriVersion.ARI_5_0_0 see the Asterisk Version to ARI Version page. (It's just easier for examples to use AriVersion.IM_FEELING_LUCKY).
Events websocket
ARI produces events that your application needs to respond to this is how
ari.events().eventWebsocket("test-app").execute(newAriWSCallback<Message>() {newAriWSCallback<List<Channel>>() {
@OverridepublicvoidonSuccess(List<Channel> result) {
// code to handle the result ...
}
@OverridepublicvoidonFailure(RestExceptione) {
e.printStackTrace();
}
@OverridepublicvoidonConnectionEvent(AriConnectionEventevent) {
// if you wish to know the status (connected/disconnected) of the WS connection
}
});
There is also an AriWSHelper abstract class that helps you get all the events as separate methods.
It's also advised to offload the work to an ExecutorService (aka thread pool) to avoid the Netty WorkerGroup exhaustion.