Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket io, node js, Simple example to send image/files from server to client

Is there any plain and straight forward examples on how to serve an image? from server to client? through buffering or just a direct call to download? (the goal is to get image files in near real time efficiently to sort of present a near live stream of images) and append to a html image tag or just in the body of the html page.

incomplete sample code: (mostly acquired from official sample or just codes from stackoverflow)

index.js

// basic variables var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http);  var fs = require('fs'); // required for file serving  http.listen(3000, function(){   console.log('listening on *:3000'); });  // location to index.html app.get('/', function(req, res){   res.sendFile(__dirname + '/index.html'); });  // only to test chat sample code from sample io.on('connection', function(socket){    console.log('a user connected');     // broadcast a message   socket.broadcast.emit('chat message', 'System Broadcast Message: a user has been connected');   socket.on('chat message', function(msg){     io.emit('chat message', msg);   });  // trying to serve the image file from the server io.on('connection', function(socket){   fs.readFile(__dirname + '/images/image.jpg', function(err, buf){     // it's possible to embed binary data     // within arbitrarily-complex objects     socket.emit('image', { image: true, buffer: buf });     console.log('image file is initialized');   }); }); 

(client side html page) index.html (we'll cut to the chase with only the portion which serves the image) What can we do on the client side to get the file and serve the image on the html page?

  socket.on("image", function(image, buffer) {      if(image)      {          console.log(" image: from client side");          // code to handle buffer like drawing with canvas** <--- is canvas drawing/library a requirement?  is there an alternative? another quick and dirty solution?         console.log(image);         // what can we do here to serve the image onto an img tag?      }   }); 

thank you for reading


Update:

after the code snippets from below it also needed to change "buffer" variable to image.buffer in order for the image to display correctly

basically change the line from

img.src = 'data:image/jpeg;base64,' + buffer; 

To

img.src = 'data:image/jpeg;base64,' + image.buffer; 
like image 260
Bigs Avatar asked Oct 13 '14 01:10

Bigs


People also ask

How to create one chat window using socket Io in Node JS?

For using the socket.io we have to use ‘socket.io’ module in js file. For understanding we have an example. First of all you have to install socket.io from npm as shown in the below command. Now we ahave an example for understanding socket.io so we have created one chat window using socket.io in Node.JS.

How to use Socket Io in JS?

For using the socket.io we have to use ‘socket.io’ module in js file. For understanding we have an example. First of all you have to install socket.io from npm as shown in the below command.

Can I send binary data over socket Io?

Web Sockets and Socket.IO support binary transfer. Additionally, why would you want to send it over socket.IO? A normal HTTP request will work fine. Also, while you can shove that binary data in your MongoDB database, it's rarely the best solution.

How to create a client in NodeJS-Express?

So far we have created a NodeJS-Express server which will listen to connections from socket.io. Now lets create the clients. Create a new HTML file called “sender.html” and create a normal form with one input field as message as shown below. We are doing nothing special here.


1 Answers

One possible solution is to base64-encode the image data and use that in the browser via image.src:

On the server side, try changing this:

socket.emit('image', { image: true, buffer: buf }); 

to this:

socket.emit('image', { image: true, buffer: buf.toString('base64') }); 

Then on the client side:

var ctx = document.getElementById('canvas').getContext('2d');  // ...  socket.on("image", function(info) {   if (info.image) {     var img = new Image();     img.src = 'data:image/jpeg;base64,' + info.buffer;     ctx.drawImage(img, 0, 0);   } }); 
like image 59
mscdex Avatar answered Sep 20 '22 02:09

mscdex