I have a service continuously sending data through a socket.
The data captured after a while looks like this:
{base64} \n {base64} \n {base64} ...
Where {base64} represents an image data.
How can I control the data flow so that I can get one image at a time (in the right order) and do something useful with it.
Right now I just pipe it to a file, but I'm sure there is a better way than to start parsing the file for delimiters and actually implement the back-pressure mechanism myself.
var streamRaw = fs.createWriteStream('raw.data');
socket.pipe(streamRaw, {end:false});
I would reference substack's example here:
var offset = 0;
socket.on('readable', function () {
var buf = socket.read();
if (!buf) return;
for (; offset < buf.length; offset++) {
if (buf[offset] === 0x0a) {
//you have buffered a full image as a base64 string,
// process it, or emit it as an event
processImageData(buf.slice(0, offset).toString());
buf = buf.slice(offset + 1);
offset = 0;
socket.unshift(buf);
return;
}
}
socket.unshift(buf);
});
You could also think of you stream as a Transform stream that reads a socket's chunks of data and emits whole sets of base64 image data as they arrive.