Constant connection to server online

214 Views Asked by At

So, I've been looking into online games and I was wondering how they managed to have such a fast, seemingly constant connection to the server. I've already tried just sending a bunch of XMLHttpRequests, but these tend to be slow. I've also tried researching, but most articles on the topic talk about things like web sockets, tcp and udp, concepts I don't understand at all. I'm also trying to do this all without importing code from an external source so I can better understand how it all works. Please help if you can. Thanks!

1

There are 1 best solutions below

0
Myst On

Your question is, by far, too broad for this platform.

I will answer what I can, briefly.

AJAX / XMLHttpRequest

The XMLHttpRequest API will (often) open a new connection to the host each time data is sent. This is a very expensive operation and will hurt performance.

Another point to consider is that an XMLHttpRequest will always send authentication and other headers, wasting both bandwidth and CPU cycles on both client and server.

There is also the issue that this approach is often based on polling, so data is sent and processed even if there's no change or update. This is not only a waste, but it might hurt other clients that are waiting for updates.

Finally, AJAX / XMLHttpRequest will use a TCP/IP connection, which is great if you must make sure that the whole of the data arrived in order but incurs a performance hit.

WebSockets

WebSockets is a protocol that runs over a streaming connection, such as TCP/IP connections (or pipes or unix sockets).

Once the connection is established, it's persistent and it allows two-ways communication. This allows both client and server to initiate an update without polling and without the extra cost (headers, re-authentication, etc').

WebSockets still sits over a streaming socket, such as a TCP/IP socket, which incurs a performance const in exchange for data integrity (all the data arrives in order).

TCP/IP sockets

TCP/IP is a streaming protocol rather than a message based protocol. For this reason it's often used with another protocol (such as WebSockets or a custom authored protocol).

Since WebSockets will use TCP/IP 99.9% of the time (unless on the same machine or on custom-made-special occasions), the same issues I described for WebSockets apply here.

However, by using a custom protocol with TCP/IP, some performance optimizations can be achieved (at a price related to network connectivity and intermediary interference).

UDP (Datagram)

Many games don't require data integrity. It doesn't matter if some packets are lost, it is more important that the latest information arrives as fast as possible.

This is where UDP sockets come in. There's no high-cost connection negotiation stage (as far as round-trips go, UDP is practically free) and there's no data integrity testing layer that might hold-up data that arrived out of order.

For these reasons, some games require UDP sockets for fast updates, while still using a TCP/IP connection for data that must arrive safely.