Alwin Brauns
Alwin Brauns

Reputation: 159

Socket.io does not emit if the data is too big

I've build a webapp that can send images. For small pictures everything works fine. But as soon as it gets a little bigger ~1000x1000px, socket.io does not emit. It does not even state an error. It just does not send the image.

This is what my Object looks like:

chatBlock = {
username: "",
message: "",
date: new Date(),
file: null,
imgWidth: 0,
imgHeight: 0,
};

and thats how I send it:

socket.emit('msgSend', chatBlock);

So actually pretty basic, but as soon as the image (named file in the object) gets to big, it does not send it.

Upvotes: 5

Views: 4121

Answers (2)

JRichardsz
JRichardsz

Reputation: 16505

As the the official docs says you should use maxhttpbuffersize

In case of express is being used:

const express = require('express');
const app = express()
const server = require('http').Server(app)
const io = require('socket.io')(
  server,{
    maxHttpBufferSize: 1e7
  }
)

But the value is something martian. It is called SI prefixes

The SI prefix mega represents a factor of 106, or in exponential notation, 1E6.

Here some values to use as maxhttpbuffersize

Here some values : https://physics.nist.gov/cuu/Units/prefixes.html

enter image description here

PD: I did not find how to set 1.5 mb

Upvotes: 1

Robert Kawecki
Robert Kawecki

Reputation: 2438

Socket.io has a message size limit: see option maxHttpBufferSize in the docs at https://socket.io/docs/v4/server-options/#maxhttpbuffersize . You can increase this limit, but keep in mind that parsing images as JSON is not very efficient. In the long term, consider uploading the images separately - a common solution is to use HTTP POST, obtain the uploaded URL, and send that to the recipient instead. This is more complex, but avoids the message size problem entirely, as file uploads can be of any size and still be streamed in an efficient manner.

Upvotes: 6

Related Questions