PHP sockets, permanent socket connection to server

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

PHP sockets, permanent socket connection to server

Post by batfastad »

I'm looking to write a PHP script to act as a mini "daemon" to receive data from a remote socket. The remote server is an Asterisk VoIP server and I'll be connecting to the Asterisk Management Interface (AMI) in an attempt to receive AMI Event notifications. The connection will be through an always-on SSH tunnel (using autossh) which has been stable enough for our use so far.

Here's the plan...
  • A PHP script connecting to the local port of the SSH tunnel which forwards to the remote port at the other end using fsockopen() or most likely pfsockopen()
  • The PHP script will be run from CLI and I guess I should have some sort of shell script on a cron job to check that the PHP script hasn't stopped for any reason
  • I'll need this PHP script to be running permanently, and permanently connected to the socket to receive data whenever it's published by the other end
  • Memory and CPU is not a problem as we have plenty of resources on our intranet server (criminally under used). But equally I don't want this script spiralling out of control
  • The PHP script will hopefully react to occasional data appearing at the other end of the socket and sometimes inserting or updating data in a MySQL database. Obviously I'll open/close the MySQL connection when necessary, not just leave it hanging.
First of all, is this a terrible idea that will never work?

Are there any PHP functions that can spring into action when data is published at the other end of the socket?

Or would I just loop using fread() like this...

Code: Select all

while (!feof($socket)) {
$output .= fread($socket, 8192);
}
The loop option seems a bit of a messy way of doing things so I'm just wondering if there's another way that will mean the script stays connected to the socket but basically idle until some data appears.

What cons/pitfalls should I be aware of when thinking about having a permanently running PHP script connected to a socket?

Cheers, B
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: PHP sockets, permanent socket connection to server

Post by VladSun »

I have to think about your solution ... a little busy today :(

I've played a lot with Asterisk, even posted a topic about it - viewtopic.php?f=14&t=106873&p=569566#p569566 , hope it helps somehow.

Not sure what you want to write in the SQL - is it a CDR or something else?
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Re: PHP sockets, permanent socket connection to server

Post by batfastad »

The data I'll be writing into a MySQL database will be the status of extensions connected to the system eg: available, busy (on call), DND, offline.
Through the Asterisk Management Interface I should be able to receive Event notifications which tell me when someone is connected/disconnected from a call, when someone has DND enabled/disabled.
I'm then planning to output this information to our intranet so users can see who are on calls.

Apparently this is an easier method than using SIP's built-in SUBSCRIBE/NOTIFY mechanism :roll:

I believe the Flash Operator Panel uses the same method, receiving AMI notifications via a socket connection.

But mainly I'm looking for info on what the best methods for doing this with PHP sockets.
It seems to me like it's better to have a PHP script that's constantly running and receiving this info as it happens, than to have a PHP script on a cron job that connects and polls for the status every 10-30s or so. But I might be wrong ;)

Cheers, B
Post Reply