Garry's Mod Wiki

Networking Options, Limits and Errors

This page lists which Networking Options there are and how they work, all Networking Limits and all found Networking Errors and how to solve them

Networking Options

umsg (DEPRECATED)

The umsg library is a deprecated serverside networking library that was previously the most common way of sending information from the server to the client. In order for clients to receive an umsg, it has to use the usermessage library, which is shared for the client and the server.

This library was used in some functions to send a message like text to the client and then to display it, and it is still used in some old functions like PrintMessage, Player:PrintMessage and Player:ChatPrint

Hello World example with the umsg and usermessage library.

if SERVER then local filter = RecipientFilter() filter:AddAllPlayers() umsg.Start( "Example" ) umsg.String( "Hello World" ) umsg.End() else usermessage.Hook( "Example", function( msg ) print( msg:ReadString() ) end) end

net

The net library is one of the ways to send data between client and server. One of the major advantages of the net library is the ability to send data backwards - from the client to the server.

This library is commonly used in the network of values to a specific client only, or to network bigger values like Strings(net.WriteString) or Ints(net.WriteInt).
You can read more about this library here: Net_Library_Usage and Net_Library_Example

When the OnRequestFullUpdate Game_Events is called, net messages will reliably be received

Hello World Example with the net library

if SERVER then util.AddNetworkString( "Example" ) net.Start( "Example" ) net.WriteString( "Hello World" ) net.Broadcast() else net.Receive( "Example", function() print( net.ReadString() ) end) end

NW

The NW system allows for a value to be networked on an entity to all clients with the SetNW functions like Entity:SetNWString and the value can be returned by using the GetNW functions like Entity:GetNWString.
It also allows to set global values with the SetGlobal* functions like SetGlobalString and the value can be retured by using the GetGlobal* functions like GetGlobalString

the value will be the same on all clients if they haven't been modified by the client.
All SetNW* functions will network the value every 10 seconds after it has been set.
All NW values will be accessible clientside when the GM:InitPostEntity hook is called
All SetGlobal* functions will network the value every 10 seconds after it has been set.
Values set with the SetGlobal* functions can be accessed with Entity(0):GetNW*

Hello World Example with the NW system

if SERVER then Entity( 1 ):SetNWString( "Example", "Hello World" ) else Entity( 1 ):SetNWVarProxy( "Example", function(_, _, _, value ) print( value ) end) end

If you want to network a table with the NW system, you can use util.TableToJSON to make it into a string, and then you can send it with Entity:SetNWString or SetGlobalString and return it with Entity:GetNWString or GetGlobalString

The string returned by util.TableToJSON should not be bigger than 255 characters. You can use the NW2 system to use up to 511 characters.

Example

Networking a Table containing Hello World

if SERVER then local table = { "Hello World" } SetGlobalString( "Example", util.TableToJSON( table ) ) else PrintTable( util.JSONToTable( GetGlobalString( "Example" ) ) ) end
Output:
1 = Hello World

NW2

The NW2 system is the successor to the NW system, but it hasn't been officially finished. It allows for values to be networked on an entity to all clients in the entity's PVS(Potential Visibility Set) with the given value with the SetNW2* functions like Entity:SetNW2String and the value can be returned by using the GetNW2* functions like Entity:GetNW2String.

The value will only be updated clientside if the entity is or enters the client's PVS(Potential Visibility Set).
The value will only be networked if it isn't the same as the current value and unlike SetNW* the value will only be networked once and not every 10 seconds.
All NW2 values will be accessible clientside when the GM:InitPostEntity hook is called
Values set with the SetGlobal* functions can be accessed with Entity(0):GetNW*

It also allows to set global values with the SetGlobal2* functions like SetGlobal2String and the value can be returned by using the GetGlobal2* functions like GetGlobal2String. All SetGlobal2* functions will update the value clientside, and it will ignore the PVS.

Hello World Example with the NW2 system

if SERVER then Entity( 1 ):SetNW2String( "Example", "Hello World" ) else Entity( 1 ):SetNW2VarProxy( "Example", function( _, _, _, value ) print( value ) end) end

or

if SERVER then Entity( 1 ):SetNW2String( "Example", "Hello World" ) else hook.Add( "EntityNetworkedVarChanged", "Example", function( _, _, _, value ) print( value ) end) end

If you want to network a table with the NW2 system, you can use util.TableToJSON to make it into a string, and then you can send it with Entity:SetNW2String or SetGlobal2String and return it with Entity:GetNW2String or GetGlobal2String

The string returned by util.TableToJSON should not be bigger than 511 characters.

Example

Networking a Table containing Hello World

if SERVER then local table = { "Hello World" } SetGlobal2String( "Example", util.TableToJSON( table ) ) else PrintTable( util.JSONToTable( GetGlobal2String( "Example" ) ) ) end
Output:
1 = Hello World

Networking Limits

umsg

The umsg library has a 256 bytes limit per message.


net

The net library has a 64kb (65535 bytes - 3 bytes used internally) limit per message. The library has an internal buffer that has roughly a 256kb(2048kbit) limit before it overflows, and if it overflows, it will cause clients that receive the net message to disconnect. So do NOT fill the buffer.
It also has clienside a reliable stream which will overflow if too many net messages are queued and the total size of all net messages is over 256kb(2048bit).

Example

Creating a net message that uses the maximum size

local string = "" for k=1, 65532 do string = string .. "a" end net.Start( "test" ) net.WriteString( string ) net.Broadcast()
Output:
65536 524288

net bandwidth

reliable channel

the bandwidth for the reliable channel is roughly at 120kb/s but can vary for each client. The ping and the amount of time needed to process the net message plays a huge amount and can influence the bandwidth strongly because the net buffer could fill up because more net messages are received than processed, which can lead to the client being kicked because of a buffer overflow.

unreliable channel

the bandwidth seems to be unlimited for the unreliable channel and only seems to be influenced by the amount of time needed to send the net message. The net message won't actually be received most of the time because the buffer would be full, and it would just skip the net message.

clientside bugs (unreliable channel)

At somewhere around 12kb/s the net buffer seems to be overloaded, and you will be frozen in place because your client won't be able to receive any net message from the server while the buffer is overloaded, but you're still able to send net messages to the server. As soon as the buffer returns to a normal state, you will start to receive all reliable net messages that got delayed because the buffer overloaded.

serverside bugs (unreliable channel)

At somewhere around 12kb/s the net buffer seems to be overloaded, and the server will start to lag without any connection problems or packet loss. All inputs are getting delayed and some like jumping won't work right. The server will return to a normal state when the net buffer returns to a normal state.


NW

The NW system uses a stringtable that has 4095 slots and is used by all SetGlobal*, SetNW* functions and the util.AddNetworkString function. By default, 95 slots are used. Each new key will use a slot, so if you use the same key on all entities, it will only use 1 slot.

Example

Returning the amount of used slots and the last key

for k = 1, 4096 do if !util.NetworkIDToString( k ) then print( k - 1, util.NetworkIDToString( k - 1 ) ) break end end
Output:
95 ServerName

NW2

The NW2 system uses a separate stringtable that has 4095 slots, so it is not influenced by the NW stringtable. The NW2 stringtable is used by all SetNW2* and SetGlobal2* functions. By default, the stringtable is completely empty.
Each new key will use a slot, so if you use the same key on all entities, it will only use 1 slot.
Currently, there is no way of checking the usage of the NW2 string table.

Networking Errors

realm

Host_Error: SV_PackEntity: SendTable_Encode returned false (ent 1).

This error is created when you try to network too many NW2Vars at the same time. When this error occurs, it will close the server and on your game will crash when you try to start a new game without restarting gmod first.
If you should ever get this error, you should reduce the amount of NW2Vars that you're trying to network at the same time


realm

Warning: Table networkstring is full, can't add [key]

This error is created when you exceed the NWVar limit, which is currently at 4095 slots. Reduce the amount of NWVars or consider using the net library or the SetNW* functions to solve this error.


realm

Too many NWVar names, could not add name [key]

This error is created when you exceed the NW2Var limit, which is currently at 4095 slots. Reduce the amount of NW2Vars or consider using the net library to solve this error.


realm

Error sending usermessage - too large ([key])

This error is created serverside when you exceed the umsg limit of 256 bytes. Reduce the size of your umsg to solve this error.


realm

Warning: Unhandled usermessage '[key]'

This error is created clientside when you forgot to use usermessage.Hook with the right key.

The umsg is deprecated, and you should use the net library instead.

Example

How to properly use the umsg library

if SERVER then local filter = RecipientFilter() filter:AddAllPlayers() umsg.Start( "Example" ) umsg.String( "Hello World" ) umsg.End() else usermessage.Hook( "Example", function( msg ) print( msg:ReadString() ) end) end

realm

[addon] Trying to send an overflowed net message!

This error is created serverside when you exceed the net limit of 64kb (65536 bytes). Reduce the size of your net message to solve this error.


realm

This error is created clientside when you overflowed the net reliable buffer, which has a 256kb(2048kbit) limit, and it will cause a client or all clients to be to disconnect.
To fix this error, you have to reduce the amount of net messages or the size of the net messages you send, so they won't overflow the net buffer.
You could distribute the net messages over a small period, so the net buffer has enough time to send all net messages.

You also can send unreliable net messages that won't kick a Client, but instead it will just ignore all unreliable net messages that couldn't fit in the buffer.

Example

Overflowing the reliable buffer while using unreliable net messages so the client won't be kicked.

local string = "" for k = 1, 65532 do string = string .. "a" end util.AddNetworkString( "Example" ) for k = 1, 4 do net.Start( "Example", true ) net.WriteString( string ) net.Broadcast() end
Output: Instead of kicking the client, all unreliable net messages just won't be received and the developer error:

Netchannel: failed reading message clc_GMod_ClientToServer from loopback.

will be created.


realm

[Addon] Warning! A net message ([name]) is already started! Discarding in favor of the new message! (function origin of the net message)

This error is created by the net library when in a previous net message an error occurred. This can be fixed by fixing the error that has been created in the net message before it.


realm

Netchannel: failed reading message svc_GMod_ServerToClient from loopback.

This Warning is shown when you set the developer ConVar to 1 or higher and an unreliable net message is received, and it doesn't fit in the net buffer. This can be fixed by reducing the amount of net messages sent and to distribute them over a small period.


realm

Netchannel: failed reading message clc_GMod_ClientToServer from loopback.

This Warning is shown when you set the developer ConVar to 1 or higher and an unreliable net message is received, and it doesn't fit in the net buffer. This can be fixed by reducing the amount of net messages sent and to distribute them over a small period.


realm

Warning! Trying to net.Broadcast a message '[name]' with no players on server!

This Warning is shown when you set the developer ConVar to 1 or higher, and you try to broadcast a net message while the server is empty. This Warning can be completly Ignored because it won't affect anything.


realm

loopback:send reliable stream overflow

This Error is shown when you overflow the clientside net reliable stream. When this error occurs the client won't be able to send any net messages to the server and after a while the client will be kicked because the server thinks the client timed out.
To fix this, reduce the amount of net messages you want to send to the server and distribute them over a small period, so the steam has enough time to sent everything.