Skip to content

Multiplayer ​

WebRTC ​

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

To start multiplayer in an instance use:

js
multiplayer.start('vrm_avatars') // can be any name
multiplayer.voiceChat.start()
multiplayer.avatars.start()

Multiplayer Objects ​

Syncronising and adding objects ​

js
// 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 ​

js
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 ​

js
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 ​

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

human.id ​

NameTypeDescription
idstringThe unique identifier of the peer.

human.username ​

NameTypeDescription
usernamestringThe username of the peer.

human.receivingAudio ​

NameTypeDescription
receivingAudiobooleanIndicates if the peer is currently receiving audio from us

human.sendingAudio ​

NameTypeDescription
sendingAudiobooleanIndicates if the peer is currently sending audio to us

human.receivingStream ​

NameTypeDescription
receivingStreamMediaStream | nullThe media stream being received from the peer, if any.

human.sendingStream ​

NameTypeDescription
sendingStreamMediaStream | nullThe media stream being sent to the peer, if any.

human.onData ​

NameTypeDescription
onDataArray.<function()>Callbacks to be executed when data is received from the peer.

human.onBuffer ​

NameTypeDescription
onBufferArray.<function()>Callbacks to be executed when a buffer is received from the peer.

human.onDisconnect ​

NameTypeDescription
onDisconnectArray.<function()>Callbacks to be executed when the peer disconnects.

human.onConnect ​

NameTypeDescription
onConnectArray.<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.

ParamTypeDescription
dataanyThe data to be sent.

Events ​

multiplayer.onConnect ​

NameTypeDescription
onConnectArray.<function()>An array of callback functions to be executed on connection.
js
multiplayer.onConnect.push(evt => {
	console.log('Now connected with: ', evt)
})

multiplayer.onData ​

NameTypeDescription
onDataArray.<function()>An array of callback functions to be executed on data receipt.
js
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.

ParamTypeDescription
urlstringThe 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.

ParamTypeDescription
dataanyThe data to be sent to connections.