Search code examples
javascriptnode.jssocket.iomultiplayer

2x Javascript: .split Multiplayer Performance and Possible package loss


Hey I am still new to javascript.

I am working on a multiplayer server using NodeJS, socket.io and javascript.

I am playing around with a serverscript I found on a website. It uses .split to send several strings of information to the Client. The client then uses an array to determine what to use the .split data for according to the order it received it in.

int(Socket.LastDataElement(2))
int(Socket.LastDataElement(3))

My first Question would be if there is a chance that these packages could come in the wrong order, or not be cleared properly?

Possible Result:

Health = Watermelon

Speed = Cheesecake

OR

The wrong player shoots the Bullet!


Second Question:

From my current point of view, sending .split strings sounds very smart compared to the alternative of sending Health, X, Y, Z, Attack, etc... as single packages. But does it actually save bandwidth and time? Or does it only look this way because I am a noob.

Cheers


Solution

  • First, no. They will never appear out of order (to your application). The writer hands the data to a TCP (Transmission Control Protocol) implementation, which does all the work of transporting it to the right process on the right remote machine. The reader will wait patiently wait for a distinct, in order, chunk of data then handle that. Depending on your application layer protocol (WebSockets, Sockets.io), entire datagrams may be sent, or just character data a little at a time.

    You're micro-managing. That's a question for TCP to handle. No matter the size of your message, your TCP implementation (probably at the OS level) will chunk it as it sees fit for transport. Unless you're sending a large amount of data (kilobytes to megabytes worth) and you need to be able to process it a little bit at a time, you don't need to worry about the chunking of data and packet size.

    In general, make sure that your transmissions are atomic (the minimum amount of information needed for your application to do what you want on the other side), and you won't have any problem with out of order packets, and you'll be just fine as far as bandwidth is concerned.