r/PHPhelp • u/GuybrushThreepywood • Nov 30 '25
Using PHP to read long-lived stream events
I recently I had a requirement to communicate with some hardware that use a REST api. Unfortunately a long-lived HTTP/TCP stream is also involved.
I decided to implement an ElectronJS/Node solution which I deployed on a client's machine. It works fine, but it is not fun, nor easy to maintain or expand. I am thinking about switching to a webserver running PHP.
Of course, the REST api can be easily handled by PHP. But how can I deal with the long lived streams?
Does FrankenPHP help me here? (I have never used it)
Edit - more details:
The device is an access controller - it is the server, and I want to subscribe to it's events.
The stream is a long-lived HTTP connection (called ISAPI Event Notification). Once you authenticate, you get a continuous stream of multipart XML payloads (each representing an event; e.g. card swipe)
The url usually looks like:
GET /ISAPI/Event/notification/alertStream
Authentication is basic or digest.
The response is always a HTTP response with: Content-Type: multipart/mixed; boundary=--myboundary
Every event comes in its own XML block, something like:
<eventNotificationAlert version="2.0" xmlns="http://www.hikvision.com/ver20/XMLSchema">
<eventType>accessControl</eventType>
<eventTrigger>doorCardOrCode</eventTrigger>
<serialNo>12345</serialNo>
<eventTime>2025-12-01T10:15:59Z</eventTime>
<doorNo>1</doorNo>
<cardNo>12345678</cardNo>
<status>success</status>
</eventNotificationAlert>
2
u/isoAntti Dec 09 '25
I've done something similar. My problem wasn't the php not being the connections open for an extended period of time, but that the web server expected a short lived sessions. So I have it running on cli, first under gnu screen, later a system service.
There's some work, trial/error on the closure of connection, reconnection and handling all cases, but all in all, php was a good option compared to many others. And it's always a good idea to have the php script running inside a shell loop, e.g, while true; do echo $(date +%r)": Starting.. "; php -f script.php ; echo "got out" ; sleep 7 ; done
8
u/[deleted] Nov 30 '25
Content-Type: text/event-streamCould be the starting point you need.5
u/curious-jake Nov 30 '25
I recently had a similar problem (building an app that had to process data from an HTTP stream). I ended up writing a node worker than wrote data to my Laravel app via it's API..
However, I did come across ReactPHP while I was looking. Seems to be designed for exactly this. Not advocating either way as I didn't give it much time. But might be of interest!
1
u/mnavarrocarter Nov 30 '25
So many questions.
First, who is the client and who is the server? Is the hardware the client and you are the server, or is the hardware the server and you are the client? This changes things.
It's trivial to consume a TCP stream, but it's not trivial to emit a TCP stream (specially in PHP). Also, this TCP stream is bound to be using some protocol correct? Is it server sent events, websokets, a less known one?
Once you know the details then I could guide you on how can you implement this.
2
u/mabahongNilalang09 Nov 30 '25
Try using generators.