1

I have a page streaming mjpegs. I used ffmpeg to generate the mjpegs, and it uses enough CPU that I would like to only have it run when someone is actively viewing the page. My thought was to start it with exec() however, it keeps running when I leave the page, and actually starts multiple instances if I then go back to the page.

Is there a way to kill a process when someone is no longer on a page? My only thought was to use ajax to send a keep alive signal to another program on the server which would kill the process if the signal isn't recieved for > 10 seconds, however it seems like there must be a less convoluted method for doing this.

1 Answer 1

1

There's no way to know from PHP when a user leaves the page unless you make another request from javascript, your approach of the ajax request is a good idea. You can also use the javascript event onbeforeunload to make a request when the user unloads the page to terminate the process.

Sign up to request clarification or add additional context in comments.

3 Comments

Remember Javascript can always fail and not be executed for security reasons, or not at all if the user has JavaScript turned off.
so if I start and stop the stream with javascript that will deal with someone not having it enabled... then I just have to worry about their browser completely crashing I suppose.
I realized if multiple people are watching and one leaves then that kills the feed for every one else... any ideas on working around that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.