MaiaVictor
MaiaVictor

Reputation: 52987

Are there relevant differences in sending many small packets or one large packet on socket.io?

Using socket.io, I have 2 options. I can send many small packets such as:

socket.emit("data",{515:{x:5,y:6}})
socket.emit("data",{124:{x:3,y:2}})
socket.emit("data",{415:{x:1,y:2}})
socket.emit("data",{135:{x:5,y:1}})

Or I can send one big packet:

socket.emit("data",{515:{x:5,y:6},124:{x:3,y:2},415:{x:1,y:2},135:{x:5,y:1}})

Considering those packets are sent several times per second and consist of most of the bandwidth usage of the sever of a ping-sensitive application, is there a difference on both approaches?

Upvotes: 3

Views: 985

Answers (1)

J Max
J Max

Reputation: 2381

I believe the following link does a good job explaining a packet's life-cycle:

http://www.sdsusa.com/connections/

The obvious takeaway is that with one packet all of the states (connection, acknowledgement, establish, send etc etc) happen once, as opposed to sending multiple packets those states have to happen once per packet. More packets will most likely result in a longer send/receive time with more connections to be made and a larger chance of dropped packets and missed connections causing additional latency.

Most likely it won't matter which approach you go with because the difference will probably be a few ms and unnoticeable. That being said if you are many packets from many clients then your server could be flooded while trying to process 30 or 40 packets a second. Additionally if you care about order then many packets would be inadvisable since there is no guarantee as to the order your packets will arive.

Upvotes: 3

Related Questions