Connect to SpatialOS

All code examples in this section assume using Improbable.Worker.CInterop; and using Example; (the generated code namespace).

Before it can interact with the simulated world, a worker must connect to SpatialOS.

There are two ways of doing this: using the Connection and using the Locator.

Connecting a managed worker

To connect a managed worker, instantiate a Improbable.Worker.CInterop.Connection object directly. Note that Connection objects are not thread-safe.

This should be used to connect a managed worker, or a worker connecting for debugging purposes via spatial cloud connect external.

The connection is made using the receptionist service of the target deployment; the IP and port for that deployment are passed as arguments to the worker. To have these values filled in automatically, use the IMPROBABLE_RECEPTIONIST_HOST and IMPROBABLE_RECEPTIONIST_PORT placeholders in your worker's launch configuration.

Connecting an external/client worker

Use the Improbable.Worker.CInterop.Locator object to enumerate cloud deployments and connect to a chosen deployment with authentication.

This is typically used to connect clients to a cloud deployment.

Values for UseExternalIP

You need to use the correct value for the UseExternalIp field in NetworkParameters. The table below summarizes the connection possibilities:

Using Connection directly Using Locator
UseExternalIp == true Local client connecting via spatial cloud connect external proxy External client connecting to cloud deployment
UseExternalIp == false Managed cloud worker, or local client connecting to local deployment Local client connecting to a local deployment as if it were a cloud deployment.

Connecting to a local Locator

When running the local API service, local clients can connect to local deployments using the local Locator. This allows clients to use largely the same code when connecting to either local or cloud deployments.

When connecting to the local Locator, you need to change your connection to connect to "localhost" and your local API port (rather than "" and 443). By default the local API port is 9876 but you can also choose it when starting the local API service. You also need to enable the LocatorParameter UseInsecureConnection`, for example:

var locatorParameters = new LocatorParameters
    UseInsecureConnection = true
var locator = new Locator("localhost", localAPIPort, locatorParameters);

Example connection setup

The example below illustrates a very basic connection setup, where the function takes three arguments specifying the worker's own ID as well as the receptionist's IP and port. It uses TCP and connects using the internal IP address.

private static Connection GetConnection(string workerId, string hostname, ushort port)
  var parameters = new ConnectionParameters
    WorkerType = "MyCsharpWorker",
    Network =
      ConnectionType = NetworkConnectionType.Tcp,
      UseExternalIp = false
  return Connection.ConnectAsync(hostname, port, workerId, parameters).Get();

Worker attributes

A worker instance is assigned some static attributes when it connects to the SpatialOS Runtime:

  • a worker ID which is a unique identifier string for this particular worker instance.
  • a worker attribute that is used to determine which layer the worker instance is part of, and therefore which components it can simulate, either automatically or by the developer through ACLs.

Get the values of these configuration settings using Connection.GetWorkerId() and Connection.GetWorkerAttributes().

2019-11-25 Page added without editorial review.

Updated about a year ago

Connect to SpatialOS

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.