Main class for multiuser networking. Access via this.context.connection from any component.

About GUIDs: In Needle Engine networking, GUIDs (Globally Unique Identifiers) are used to identify objects and components across the network.
Every GameObject and Component has a unique guid property that remains consistent across all clients.
GUIDs are automatically assigned (e.g. during export from Unity/Blender) and are essential for:

  • Object ownership management (see OwnershipModel)
  • State synchronization (storing and retrieving object state)
  • Identifying which object received a network message

When working with networking, you'll typically use this.guid to identify your component or this.gameObject.guid for the GameObject.

this.context.connection.connect();
this.context.connection.joinRoom("my-room");
this.context.connection.beginListen("my-event", (data) => {
console.log("Received:", data);
});
this.context.connection.send("my-event", { message: "Hello" });
// Get state for a specific object by its GUID
const state = this.context.connection.tryGetState(this.guid);

// Delete remote state for an object
this.context.connection.sendDeleteRemoteState(this.guid);

Implements

Constructors

Accessors

  • get allowEditing(): boolean

    True when connected to a room via a regular url, otherwise (when using a view only url) false indicating that the user should not be able to modify the scene

    Returns boolean

  • get connectionId(): null | string

    The connection id of the local user - it is given by the networking backend and can not be changed

    Returns null | string

  • get currentLatency(): number

    Latency to currently connected backend server

    Returns number

  • get currentRoomName(): null | string

    The name of the room the user is currently connected to

    Returns null | string

  • get currentRoomViewId(): null | string

    The view id of the room the user is currently connected to.

    Returns null | string

  • get currentServerUrl(): null | string

    The current server url that the networking backend is connected to (e.g. the url of the websocket server)

    Returns null | string

  • get isConnected(): boolean

    Checks if Needle Engine networking is connected to a websocket. Note that this is not equal to being connected to a room. If you want to check if Needle Engine is connected to a networking room use the {@link isInRoom} property.

    Returns boolean

    true if connected to the websocket.

  • get isDebugEnabled(): boolean

    Returns true if the networking backend is in debug mode.
    To see all networking messages in the console use ?debugnet in the url

    Returns boolean

  • get isInRoom(): boolean

    True if connected to a networked room. Use the joinRoom function or a SyncedRoom component

    Returns boolean

Methods

  • Use to start listening to networking events.
    To unsubscribe from events use the {@link stopListen} method.
    See the example below for typical usage:

    // Make sure to unsubscribe from events when the component is disabled
    export class MyComponent extends Behaviour {
    onEnable() {
    this.connection.beginListen("joined-room", this.onJoinedRoom)
    }
    onDisable() {
    this.connection.stopListen("joined-room", this.onJoinedRoom)
    }
    onJoinedRoom = () => {
    console.log("I joined a networked room")
    }
    }

    Parameters

    Returns Function

  • Use to start listening to networking binary events

    Parameters

    • identifier: string
    • callback: BinaryCallback

    Returns BinaryCallback

  • Used to connect to the networking server

    Parameters

    • Optionalurl: string

      Optional url to connect to. If not provided, it will use the url from the registered INetworkingWebsocketUrlProvider or the default backend networking url. If you want to change the url after connecting, you need to disconnect first and then connect again with the new url.

    Returns Promise<boolean>

  • Disconnect from the networking backend + reset internal state

    Returns void

  • Returns a url that can be shared with others to view the current room in view only mode.
    This is useful for sharing a room with others without allowing them to modify the scene.
    Use connection.allowEditing to check if the current room is in view only mode.

    Returns null | string

  • Joins a networked room. If you don't want to manage a connection yourself you can use a {@link SyncedRoom} component as well

    Parameters

    • room: string
    • viewOnly: boolean = false

    Returns boolean

  • Use to leave a room that you are currently connected to (use leaveRoom() to disconnect from the currently active room but you can also specify a room name)

    Parameters

    • room: null | string = null

    Returns boolean

  • Send a message to the networking backend - it will broadcasted to all connected users in the same room by default

    Type Parameters

    • T extends WebsocketSendType

    Parameters

    • key: string
    • data: null | T = null
    • queue: SendQueue = SendQueue.Queued

    Returns void

  • Send a binary message to the server (broadcasted to all connected users)

    Parameters

    Returns void

  • Returns void

  • Deletes the network state for a specific object on the server.
    This removes the object's state from the room, preventing it from being sent to newly joining users.

    Parameters

    • guid: string

      The unique identifier of the object whose state should be deleted

    Returns void

    // When destroying a networked object, clean up its server state
    onDestroy() {
    this.context.connection.sendDeleteRemoteState(this.guid);
    }
  • Use to delete all state in the currently connected room on the server

    Returns void

  • A ping is sent to the server at a regular interval while the browser tab is active. This method can be used to send additional ping messages when needed so that the user doesn't get disconnected from the networking backend

    Returns void

  • Use to stop listening to networking events
    To subscribe to events use the {@link beginListen} method.
    See the example below for typical usage:

    // Make sure to unsubscribe from events when the component is disabled
    export class MyComponent extends Behaviour {
    onEnable() {
    this.connection.beginListen("joined-room", this.onJoinedRoom)
    }
    onDisable() {
    this.connection.stopListen("joined-room", this.onJoinedRoom)
    }
    onJoinedRoom = () => {
    console.log("I joined a networked room")
    }
    }

    Parameters

    • key:
          | string & {}
          | OwnershipEvent
          | OwnershipEventNamesIncoming
          | RoomEvents
          | RoomEventsIncoming
    • callback: null | Function

    Returns void

  • Use to stop listening to networking binary events

    Parameters

    • identifier: string
    • callback: any

    Returns void

  • Parameters

    • key:
          | string & {}
          | OwnershipEvent
          | OwnershipEventNamesIncoming
          | RoomEvents
          | RoomEventsIncoming
    • callback: null | Function

    Returns void

    please use stopListen instead (2.65.2-pre)

  • Returns the cached network state for a given GUID.
    The state is stored locally whenever network updates are received for that object.

    Parameters

    • guid: string

      The unique identifier of the object whose state you want to retrieve

    Returns null | IModel

    The cached state object, or null if no state exists for this GUID

    // Get the last known state for this component
    const myState = this.context.connection.tryGetState(this.guid);
    if (myState) {
    console.log("Found cached state:", myState);
    }
  • Returns true if a user with the given connectionId is in the room

    Parameters

    • id: string

    Returns boolean

  • Returns a list of all user ids in the current room

    Parameters

    • target: null | string[] = null

    Returns string[]