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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With