reactphp / stream

Event-driven readable and writable streams for non-blocking I/O in ReactPHP.
https://reactphp.org/stream/
MIT License
618 stars 62 forks source link

"Always on" connection that streams data "bundles" #143

Closed sburkett closed 5 years ago

sburkett commented 5 years ago

Hi everyone! Fairly new to ReactPHP, but it's pretty cool from what I've seen thus far. I find myself in a situation where unfortunately my working knowledge of ReactPHP isn't quite enough yet to solve my problem. I'm hoping this is the correct component in which to post this in ... if not, let me know!

NOTE: I know this isn't really an "issue", but more of a question. I hope that is ok.

Our customer is sending us data in "bundles" of data that look sort of like this:

MESSAGE1 ... BLAH BLAH.... FIELDS HERE.... YADA YADA.... ^M
MESSAGE2 ... BLAH BLAH.... FIELDS HERE.... YADA YADA.... ^M
...

*** Yes, those are carriage returns, not linefeeds, so the data all comes in as one big line.

Now, the way we used to do it with a simple socket server over a VPN. They would connect, send us the stuff, and then disconnect. No biggie there.

However, now they want an "always on" socket connection" (don't ask), where they can send us data without the overhead of a connection, etc.

So in other words, the socket connection is always open, and data could come in at any time. We may get some data one minute, and then here may be a pause of some time before we receive another bundle of data.

For each bundle of data we get, we need to send them a message back acknowledging receipt of the bundle. The good news is that the "bundle" of data does have a footer that I can key off of to denote the end of the bundle.

I'm guessing that a Duplex stream would be the way to go, whereby we both receive and send data on the same stream (in this case using a TCP socket).

However, I'm not quite sure how best to approach this using ReactPHP, as again, I'm still very new to it. Any guidance or suggestions would be greatly appreciated!

sburkett commented 5 years ago

Ah, never mind. I ended up arriving at the solution tonight. Slick package!

clue commented 5 years ago

@sburkett Glad to hear you've got this sorted out already! Your initial message already sounds like you seem to have a good understanding of how ReactPHP works: a parser consuming the readable side of a stream for your particular protocol that buffers some chunks until it reaches an end of message indicator is usually the way to go. Do you care to share your solution in case anybody else happens to stumble upon this? :+1: