Asterisk Manager Asynchronous TCP/IP Connection

Good Morning!

Experimenting a bit this morning with AMI.

Right now, we’re listening to events and occasionally opening up another socket to AMI to perform an action.

It dawned on me that I could use the same connection to send commands as I am using to receive events.

This seems to work for the very first event and command, and then AMI becomes unresponsive.

Is what I’m attempting possible? Can you re-use the same socket to listen to events and send commands?

Asterisk 20.1.0-rc2

BK

People do it - but some commands are not asynchronous and will block the socket until they complete. It depends on what exactly you’re doing.

1 Like

Interesting, so here’s what I’m testing after opening the socket:

    Action: Login\r\n
    UserName: someusername\r\n
    Secret: somesecret\r\n\r\n
    Action: Events\r\n
    EventMask: call\r\n\r\n

And then subsequently on the same socket:

    Action: Login\r\n
    UserName: someusername\r\n
    Secret: somesecret\r\n\r\n
    Action: MixMonitor\r\n
    Channel: PJSIP/somechannel\r\n
    File: somefile\r\n
    Options: someoptions\r\n\r\n
    Action: Setvar\r\n
    Channel: PJSIP/somechannel\r\n
    Variable: SOMEVARIABLE\r\n
    Value: somevalue\r\n\r\n
    Action: Logoff\r\n\r\n 

I’m listening for BridgeEnter/BridgeLeave to trigger sending MixMonitor/StopMixMonitor.

Seems to work great for the very first BridgeEnter, MixMonitor starts the recording, but then nothing on the socket and no errors, verified the socket remains open.

I’ve tried with and without Login/Logoff on the actions.

Note this is a known-working daemon that I built, the only thing I’m doing different is using the same socket vs opening a new socket for the actions.

Any insight appreciated :smiley:

BK

I don’t really have any further insight, I rarely use AMI and issues regarding it seldom come up. Someone else may have thoughts.

1 Like

I don’t think you should be using Login on a logged in connection.

You definitely shouldn’t expect to continue to receive events after the Logoff.

I provide a number of different example listener scripts — sync versus async, AMI versus ARI — in my examples repo for Seaskirt. Feel free to use that as a reference. And let me know if they don’t work for you.

2 Likes