I am creating a small application with nodejs that pass data in multipart / x-mixed-replace to the browser.
This data is created with image data, but image data can change over time, so in a browser it looks like a video. Image data is created from a webcam, so in a browser it looks like streaming video.
But the performance is not very good.
I tried other approaches: - First: use socket.io to move images to the browser, here I use base64 data from the image (click this data), and in the browser I recreate the image (jpeg): it works fine, but only with one or two by customers. _ Second: Use polls from the browser to the nodejs server. I do not like it.
So this is the code: (some part of my nodejs host code) I use express to create an http server:
app.get('/videoStream',function(req,response){
response.writeHead(200,{
'Content-Type': 'multipart/x-mixed-replace;boundary="' + boundary + '"',
'Connection': 'keep-alive',
'Expires': 'Fri, 01 Jan 1990 00:00:00 GMT',
'Cache-Control': 'no-cache, no-store, max-age=0, must-revalidate',
'Pragma': 'no-cache'
});
response.write('--'+boundary+'\n');
events.addListener('imagen_recibida',function(){
fs.readFile(__dirname + '/image.jpeg',function(err,data){
if(err) return send404(response);
response.write('Content-Type: image/jpeg\n Content-Length: '+data.length+'\n\n');
response.write(data);
response.write('\n--'+boundary+'\n');
});
});
When the event "imagen_recibida" is incremented, it reads the image from disk and writes data to the browser.
So, two questions:
Is there any approach to increase the effectiveness of this? (write the image to disk, and then read to send to the browser does not see how a good trick)
Is there a way to encode this to a different format for better performance?
Many thanks.
PD: , , RPC.