Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

websocket client disconnects on sending large data

I'm having nodejs websocket server and client to send images. Problem is client auto disconnects on sending high resolution [i.e 512 x 512 or more] or large size [i.e nearly > 50KB]images.

Note: With lesser dimensions and size it's able to send.

function getBase64Image(img) {
  var canvas = document.createElement("canvas");
  canvas.width = img.width;
  canvas.height = img.height;

  var ctx = canvas.getContext("2d");
  ctx.drawImage(img, 0, 0);

  var dataURL = canvas.toDataURL("image/png");
  return dataURL;
 }

function sendImage(){
  var img = getBase64Image(document.getElementById("image") );
  var JSONimg = {
   ' type' : 'img',
   'data' : img,
  }
 ws.send(JSON.stringify(JSONimg));
 }

Corresponding HTML:

 <button onclick="sendImage()">Send Image</button>
 <img id="image" src="img/image.png" alt="" />

Question

Is Size or dimension of image is a reason for socket disconnection?

Any Size limitation for websocket messages?

like image 492
Pushpa Avatar asked Mar 26 '15 18:03

Pushpa


2 Answers

As described in this Q&A, a WebSocket message has no size limitation at the protocol level. Nevertheless, most implementation has its own limitation on message size due to memory issues. So you should look at documentation of WebSocket implementation you use.

BTW, WebSocket protocol allows to exchange binary message directly without binary-to-text conversion which has some performance penalty as well as increases message size.

like image 101
Donghwan Kim Avatar answered Nov 03 '22 14:11

Donghwan Kim


I found solution to this.The problem here is with the data size. Web socket protocol has its limitation on message size. Better follow RFC standards before implementation. Explained here rfc6455#section-5.2

like image 21
Pushpa Avatar answered Nov 03 '22 13:11

Pushpa