Re: Persistent connections



On Jul 28, 5:09 pm, Joseph <northwest...@xxxxxxxxx> wrote:
I have a PHP script that opens up a socket, and reads data.

OK

This is a
persistent socket.

No such thing for sockets. If you mean you wish it would behave like a
persistent database connection, then that's something different - and
would require changes to the source code for the PHP engine to manage
a connection pool - but note that it will only process data when a PHP
scripts a connction from the pool and starts using it.

The problem is that if I launch it from my browser,
I have to keep my browser open.

I infer from this that you mean you need to process data from the
socket continiously (which is very bad idea for a process tied to a
webserver) and that you don't need to see remotely what its doing in
the same (process) session as the script is executing. (it's usually
helpful when asking about the best way to solve a problem, what the
constraints are as well as the objectives). Also, you don't state if
your PHP code is running as server or client - for the sake of
brevity, I'll assume the latter.

If I launch it as a CRON job, then I
will disconnect every time the cron job runs.

No.

1) it's not you that disconnects, it's the server or the client
2) there's nothing intrinsic to sockets which would cause a
disconnection merely because it is started by cron
3) there's nothing intrinsic in sockets which would cause a
disconnection merely because additional instances were started (one of
the reasons for using sockets / servers is specifically to avoid this
problem).

Certainly it might be that you don't want more than client operating -
but that could be solved by using mutexes at either end of the
connection.

Any suggestions?

Lots. It would have been helpful to know what OS you're running on,
but you could try:

1) setting up the client to run as a daemon - have a google for long
running php process for more info - but beware that there's a lot of
crap written about this - anyone who is describing a solution for
POSIX and not refering to process groups and session headers probably
doesn't know what they're talking about. (hint: $run=`echo /usr/bin/
php -q $client_script | at now`;)

2) start the script from init (which also provides a watchdog service
to respawn it if/when it fails)

3) use a daemon wrapper such as djbtools

4) use a generic socket client (e.g. nc) to store the raw data (see
also 2,3,4 above)

C.
.



Relevant Pages