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

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.

A bit hard to understand you first question, but....

From my experience with socket.io, the messages always arrive in the same order that they are sent by the client, and you never lose any messages. However, there could be a situation where, say, 10 messages are sent, spread out over 2 seconds, but due to latency issues, get received on the other in rapid succession of 10 messages – as if they all went out simultaneously (again, the order is still preserved). Depending on what's happening inside your game, sometime this doesn't matter. But other times you actually care about how much time elapsed between each message being sent out (like did a player press the trigger 10 times in 2 seconds or 10 times in 5ms). And then you're forced to do stuff like time-stamping the messages and making sense of the time stamps on the other end.

As for the second question: Without knowing much about the underlying implementation of socket.io or, further down, WebSockets, I'm still guessing the more you lump into a single message the better off you are – if not in terms of bandwidth, then at least in terms of performance and latency.

Within javascript, the handler function (the one that processes the json received) gets called once per message, so lumping the messages ensures the handler is called fewer times.