No there isn't a better way.
It's the very nature of how PHP works. It's a reactive language. When you make a request to a php script, it runs and then stops. Thats it because thats the way http works. Http doesn't keep a connection open and continue swapping request data and html. It's a request and reply service.
I'm afraid the only thing you can do is to create a cycling script which keeps the socket open in a loop and communicate with it via another script or use multiple page requests.
There really isn't a miracle method here to doing it. It's just not designed that way.
My helpful sig has gone because a mod below the administrator gave me an infraction - despite the administrator personally agreeing to it.
If you need any php tips or tricks you can PM me.