iPhone and Node.js, multiple packets in single read

Problem

I feel like I’m doing this wrong, or it’s just a result of my novice with sockets. I’m using asyncSocket on the iPhone to send data to node.js. My problem is that every so often, the app will hang, and then I’ll get like 5 or 10 messages at once in one data read, rather than separate, more timely reads.

Is this a network hiccup and the data backed up or is this node.js trying to be smart on my behalf and group together very quick messages? Is there something I can send on the iPhone side to say “this is the end of my message, stop waiting for more data” so I can be sure it’s a network issue? I feel like there should be a termination character, like asynsocket uses new lines, but I can’t seem to make that work with node.

It’s much more important to me that the data arrives quickly than anything else.

Thanks!

EDIT: been trying to use 0xFF but I’m not sure I’m doing that right, and I don’t have anything to test against, so I have no way to know.

...
[asyncSocket writeData:data withTimeout:-1 tag:0];
NSData* term = [NSData dataWithBytes:"\xFF" length:1];
[asyncSocket writeData:term withTimeout:-1 tag:0];

Problem courtesy of: UltimateBrent

Solution

As a fellow recent socket novice I can say that my noodle was well and truly baked by regular, boring Unix sockets (in PHP?!), so much so that I’d be afraid to approach the whole libevent thing too soon!

One thing I can say is that it’s the job of the protocol to signal the end of a message, the underlying TCP pipe seems to be completely asynchronous.

HTH

Solution courtesy of: Robin

View additional discussion.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.