Skip to main content
s&box networking is designed to be simple and easy to use. The goal is to get multiplayer games up and running quickly without requiring deep networking expertise.
The networking system in s&box is purposefully simple. The initial aim is not to provide a bullet-proof server-authoritative networking system, but to provide a system that is easy to use and understand.

Creating and joining lobbies

Use the Networking static class to create, list, and join lobbies.

Create a lobby

Networking.CreateLobby( new LobbyConfig()
{
    MaxPlayers = 8,
    Privacy = LobbyPrivacy.Public,
    Name = "My Lobby Name"
} );

List available lobbies

var list = await Networking.QueryLobbies();

Join a lobby

Networking.Connect( lobbyId );

NetworkHelper

The NetworkHelper component handles the most common multiplayer setup for you. Add it to a GameObject in your scene to:
  • Automatically create a server when the scene loads (if StartServer is enabled).
  • Spawn a player prefab for each connection that becomes active.
  • Assign ownership of the spawned object to the connecting client.
Set the PlayerPrefab property to your player prefab and optionally add a list of spawn point GameObjects. If no spawn points are defined, players spawn at the NetworkHelper object’s location.
protected override void OnUpdate()
{
    // If we're a proxy then don't do any controls
    // because this client isn't controlling us!
    if ( IsProxy )
        return;

    if ( !Input.AnalogMove.IsNearZeroLength )
    {
        WorldPosition += Input.AnalogMove.Normal * Time.Delta * 100.0f;
    }

    var camera = Scene.GetAll<CameraComponent>().FirstOrDefault();
    camera.WorldRotation = new Angles( 45, 0, 0 );
    camera.WorldPosition = WorldPosition + camera.WorldRotation.Backward * 1500;
}
NetworkHelper implements Component.INetworkListener under the hood. See Network events for how to implement your own listener.

Testing multiplayer locally

The best way to test multiplayer is to have another player join, but you can also spawn a second game instance that joins your current session.
1

Start your game

Launch your game in the s&box editor as normal.
2

Open the network status menu

Click the network status icon in the header bar and select Join via new instance.
3

Second instance joins

A new game instance launches and automatically joins your running session.
You can keep coding in your main instance while the second instance is connected. Code changes hot-reload to all connected clients automatically.
To reconnect a client without restarting, run reconnect in the console. To join your local editor session manually from any instance, run connect local.

HTTP requests

s&box provides a static Http class for making asynchronous HTTP requests.
// GET request — returns the response as a string
string response = await Http.RequestStringAsync( "https://google.com" );

// POST request with JSON content
await Http.RequestAsync( "https://api.facepunch.com/my/method", "POST", Http.CreateJsonContent( playerData ) );
Only http and https URLs to domain names are allowed (no IP addresses). localhost is permitted only on ports 80, 443, 8080, and 8443. Use the -allowlocalhttp command-line switch on a server to access any local URL.

WebSockets

Use WebSockets to connect to an external server for custom networking or persistent data.
public sealed class Server : Component
{
    // Example: wss://host.example:443/ws
    [Property] public string ConnectionUri { get; set; }
    public WebSocket Socket { get; set; }

    protected override void OnStart()
    {
        Socket = new WebSocket();
        Socket.OnMessageReceived += HandleMessageReceived;
        _ = Connect();
    }

    private async Task Connect()
    {
        await Socket.Connect( ConnectionUri );
        await SendMessage( "Hello!" );
    }

    private async Task SendMessage( string message )
    {
        await Socket.Send( message );
    }

    private void HandleMessageReceived( string message )
    {
        Log.Info( message );
    }
}

Using auth tokens

Attach an auth token to the WebSocket request headers:
var token = await Sandbox.Services.Auth.GetToken( "YourServiceName" );

if ( string.IsNullOrEmpty( token ) )
{
    // Unable to fetch a valid session token
    return;
}

var headers = new Dictionary<string, string>()
{
    { "Authorization", token }
};

await socket.Connect( "ws://localhost:8080", headers );

Next steps

Networked objects

Spawn and destroy networked GameObjects, sync properties, and control visibility.

RPCs

Call functions remotely across the network with broadcast, owner, and host RPCs.

Dedicated servers

Set up and run a dedicated server for your s&box game.