Reputation: 2013
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...
fsockopen()
or most likely pfsockopen()
First of all, is this a terrible idea that will never work?
I realise PHP's probably not the best language for a sort of small daemon like this but I've had success with PHP on CLI before and it's the language I'm most comfortable with these days.
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...
while (!feof($socket)) {
$output .= fread($socket, 8192);
}
The loop option seems a bit of a mess 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
Upvotes: 1
Views: 4516
Reputation: 3412
Have a look at ZeroMQ, asynchronous message library. http://en.wikipedia.org/wiki/ZeroMQ
Here's the link to the php bindings:
http://www.zeromq.org/bindings:php
Upvotes: 1
Reputation: 20540
you should be aware of memory consumption when writing a PHP daemon. If not carefully unset()/free()d, your daemon may eat more and more memory over time.
as you already said, PHP is not the best language for this, but it will definitely work. i already did that "hack" a few times.
be aware that fread() on a TCP socket will be non-blocking. As each line is CR/LF terminated (\r\n) and two CR/LF mark the end of an AMI event/command, you may want to read until "\r\n\r\n" occurs, then process this event and then begin reading again. i'd set the socket blocking and use fgets() instead of fread(). that way its much easier to detect the end of an AMI event - without needing to do string magic / splitting.
Upvotes: 1