A single keep alive connection between php script and a client/computer

i’d like to make a set up where a piece of software running on a computer connects to the webserver and sends very small bits of data intermittently to the webserver and i’d like to have it a persistant connection – one connection for, say, one day. i want to absolutely minimise the amount of data sent backwards and forwards.

on the php/server side of things what would i need to do? i know there’s the max_execution_time setting so that would need to be high enough. but how would you get into this situation? just never end the code?; sleep indefinitely? and when the client sends a bit of data have some code triggered by an event to handle it maybe?

thanks.

ps: and would it be possible for any communication back to the client/computer from the server without closing the connection or not?

What kind of client? A web browser, or something else where you have more power?

For the server, I rarely see people use php for stuff like this. While you could write a daemon in php, and you can search and find examples, it’s not common. People tend to use other languages for socket servers from what I’ve seen. I’ve seen a lot of java/python/.net used for this. My guess is they’re probably more suited and reliable for such a task. I know I’ve come across more than a few “instant messenger/chat” applications in java that hold sockets open, which is basically the skeleton of what it sounds like you want to do.

not a web browser, yup something where i have more power. not sure what exactly yet. might be a script, might be a purpose written app, but code in its own right. i’ll have full control over the set up of this; it’s not for users as such.

For the server, I rarely see people use php for stuff like this. While you could write a daemon in php, and you can search and find examples, it’s not common. People tend to use other languages for socket servers from what I’ve seen. I’ve seen a lot of java/python/.net used for this. My guess is they’re probably more suited and reliable for such a task. I know I’ve come across more than a few “instant messenger/chat” applications in java that hold sockets open, which is basically the skeleton of what it sounds like you want to do.

yes instant messenger/chat like apps is a good point; they’ll have the right kind of underlying thing, assuming they do hold the socket open. yup, that’s something worth looking into to see what kind of protocol they use.

i don’t see why php wouldn’t be good for this though? i can’t see python being able to do it particularly better. could be wrong though.

live chat code – a lead to follow up, see how they do it. great, thanks.

PHP was never designed to be kept running; it was designed to run tasks quickly, in fractions of a second.

Keeping it alive is something that it was never designed for, and as such it will have inefficiencies and difficulties which won’t be present in languages meant for this kind of thing.

oh ok. i’m just starting to learn python so maybe that’s what i should use then. thanks.

johnyboy, what your looking for might be the use of AJAX (note it relies on javascript being available on the client computer).

The absolute best option would be to roll your own server and protocol (or use some serialization-powered protocol).