Multiplayer

WebRTC

Unia uses WebRTC for multiplayer data/audio and uWebSockets.js for sockets.

To start multiplayer in an instance use:

multiplayer.start("vrm_avatars"); // can be any name
multiplayer.voiceChat.start();
multiplayer.avatars.start();

Multiplayer Objects

Syncronising and adding objects

// add a multiplayer object, by default this will syncronise objects using their uuid
world.addMultiplayerObject(this);

// to specify a key for sycing set:
this.__multiplayerID = "some unique key";
// This is useful for objects created at runtime as uuid's may not be identical

// This will syncronise an object's position, rotation, scale each frame
// By default this also called with grab events
this.update = () => {
  multiplayer.setObject(object);
};

Updating properties

const keyValues = {
  objectProperty: "someValue",
  otherProperty: { x: 0, y: 1, z: 3 },
};
// After calling this the property on all peers object will set to these values
multiplayer.objects.setProperties(object, keyValues);

Calling object functions

const functionProps = {
  x: 10,
  otherProperty: { x: 0, y: 1, z: 3 },
};
// After calling this the property on all peers object will set to these values
multiplayer.objects.callFunction(object, "functionName", functionProps);

Humans

multiplayer.connections; // an array of humans you are connected with
const human = multiplayer.connections[0];
human.username; // 'string:id'

human.id

Name Type Description
id string The unique identifier of the peer.

human.username

Name Type Description
username string The username of the peer.

human.receivingAudio

Name Type Description
receivingAudio boolean Indicates if the peer is currently receiving audio from us

human.sendingAudio

Name Type Description
sendingAudio boolean Indicates if the peer is currently sending audio to us

human.receivingStream

Name Type Description
receivingStream MediaStream | null The media stream being received from the peer, if any.

human.sendingStream

Name Type Description
sendingStream MediaStream | null The media stream being sent to the peer, if any.

human.onData

Name Type Description
onData Array.<function()> Callbacks to be executed when data is received from the peer.

human.onBuffer

Name Type Description
onBuffer Array.<function()> Callbacks to be executed when a buffer is received from the peer.

human.onDisconnect

Name Type Description
onDisconnect Array.<function()> Callbacks to be executed when the peer disconnects.

human.onConnect

Name Type Description
onConnect Array.<function()> Callbacks to be executed when the peer successfully connects.

human.startVoice()

Starts sending voice data to the peer.

human.stopVoice()

Stops sending voice data to the peer.

human.sendData(data)

Sends JSON-serialized data to the peer, if connected.

Param Type Description
data any The data to be sent.

Events

multiplayer.onConnect

Name Type Description
onConnect Array.<function()> An array of callback functions to be executed on connection.
multiplayer.onConnect.push((evt) => {
  console.log("Now connected with: ", evt);
});

multiplayer.onData

Name Type Description
onData Array.<function()> An array of callback functions to be executed on data receipt.
multiplayer.onData.push((data) => {});

multiplayer.start(url)

Starts the multiplayer session. Initializes the client with a given URL and sets the project space to the current editor's project ID.

Param Type Description
url string The URL to start the multiplayer session with.

multiplayer.stop()

Stops the multiplayer session. Kills the client connection and marks the multiplayer as disabled.

multiplayer.sendData(data)

Sends data to all connected connections.

Param Type Description
data any The data to be sent to connections.