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.