Skip to main content

Network

API for network-related functions.

All network messages are identified by a 64-bit hash of their name. This hash is automatically computed when using Network.send and Network.receive, so you don't need to worry about it unless you're handling the low-level Network.message event directly.

Every network message is limited to a maximum payload size of 1 megabyte (1,048,576 bytes). All data is sent on the reliable channel.

Functions

Network.send

Sends a buffer of data to a specified player as a specific network message.

Network.send(payload: buffer, messageName: string, player: Player)

Parameters

NameTypeDescription
payloadbufferThe buffer of data to send.
messageNamestringThe name of the network message to send.
playerPlayerThe player to send the data to.

Network.send

Sends a buffer of data to the server as a specific network message.

Network.send(payload: buffer, messageName: string)

Parameters

NameTypeDescription
payloadbufferThe buffer of data to send.
messageNamestringThe name of the network message to send.

Network.receive

Registers a callback to be called when a specific network message is received from a player.

Network.receive(messageName: string, callback: fun(player: Player, payload: buffer))

Parameters

NameTypeDescription
messageNamestringThe name of the network message to listen for.
callbackfun(player: Player, payload: buffer)The callback to be called when the message is received.

Network.receive

Registers a callback to be called when a specific network message is received from the server.

Network.receive(messageName: string, callback: fun(payload: buffer))

Parameters

NameTypeDescription
messageNamestringThe name of the network message to listen for.
callbackfun(payload: buffer)The callback to be called when the message is received.

Network.getMessageNameHash

Gets the hash of a network message name.

Network.getMessageNameHash(messageName: string): string

Parameters

NameTypeDescription
messageNamestringThe name of the network message.

Returns

NameTypeDescription
hashstringThe 64-bit hash of the network message name as a string.

Network.addEventListener

Adds an event listener. A list of events can be found here.

Network.addEventListener(eventType: string, callback: function): function

Parameters

NameTypeDescription
eventTypestringThe event to listen for.
callbackfunctionThe callback to use.

Returns

NameTypeDescription
callbackfunctionThe callback that was registered.

Network.removeEventListener

Removes an event listener.

Network.removeEventListener(eventType: string, callback: function)

Parameters

NameTypeDescription
eventTypestringThe event to remove the listener from.
callbackfunctionThe callback to remove.

Events

"message"

Fired when a network message is received from the server.

  • Network messages are identified by a hash, not by name.
  • Use Network.receive to register callbacks for specific messages.
Network.addEventListener("message", fn(messageNameHash, payload) end)

Parameters

NameTypeDescription
messageNameHashstringThe 64-bit hash of the received network message as a string.
payloadbufferThe buffer of data received.

"message"

Fired when a network message is received from a player.

  • Network messages are identified by a hash, not by name.
  • Use Network.receive to register callbacks for specific messages.
Network.addEventListener("message", fn(player, messageNameHash, payload) end)

Parameters

NameTypeDescription
playerPlayerThe player who sent the message.
messageNameHashstringThe 64-bit hash of the received network message as a string.
payloadbufferThe buffer of data received.