s&box
RustCounter-Strike 2s&boxDeadlock
Wiki
Get s&box on Steam
Browse
OverviewGetting StartedCreating GamesEngine & ToolsDocumentationCommunity GamesResources
s&boxDocumentationNetworkingWebsockets
SOURCE 2 · C#
Build and play games with s&box.

Facepunch's game engine and creation platform — make games in C#, or jump straight into community games.

Get s&box on Steam

s&box Wiki

  • Overview
  • Getting Started
  • Creating Games
  • Engine & Tools
  • Documentation
  • Community Games
  • Resources

Tools & Community

  • Getting Started
  • Creating Games
  • Engine & Tools
  • Resources

Platforms

  • RustBattle
  • CSBattle
  • Rust Game Wiki
  • CS2 Skins Wiki
  • Deadlock Wiki

About

  • Official Site
  • Developer Wiki
  • GitHub
  • Get on Steam
NOT AFFILIATED WITH FACEPUNCH STUDIOS · © 2026 s&box Wiki (community)
Docs/Networking
Networking

WebSockets

s&box allows you to use WebSockets to interface with an external server. Common usages include custom networking, or persistent data outside of a local filesystem.

Adding this Component to a GameObject in the Scene will ensure the connection is started when the game starts.


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();
	}

	// Connect to the server and send a message
	private async Task Connect()
	{
		await Socket.Connect( ConnectionUri );
		await SendMessage( "Hello!" );
	}

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

	// Log our received messages
	private void HandleMessageReceived( string message )
	{
		Log.Info( message );
	}
}

You could attach an Auth Token to your WebSocket request header like so:


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 );

Source: Facepunch/sbox-docs (CC-BY-4.0) · updated 2024-09-26. Read it rendered on the official docs.

More in Networking

Connection Permissions
Custom Snapshot Data
Dedicated Servers
Http Requests
Network Events
Network Helper
Network Visibility
Networked Objects
← All documentation

Community wiki — not affiliated with Facepunch Studios. For the latest and most authoritative information, see the official developer wiki.