@workadventure/room-api-client
Easily create a GRPC client to connect your service to the Room API of a WorkAdventure server.
Installation
npm install @workadventure/room-api-client
Instantiating the client
Use the createRoomApiClient
function to create a client.
The client expects an API key as first parameter. See the Authentication section of the Room API documentation
to learn how to get your own API key.
const client = createRoomApiClient("MY AWESOME KEY");
By default, the client targets the official WorkAdventure server. If you are using a self-hosted version, you
must in addition pass in parameter the domain name and port of your WorkAdventure RoomApi endpoint.
const client = createRoomApiClient("My AWESOME KEY", "play.example.com", "5221");
Setting / Reading / Tracking variables
The Room API client allows you to set, read and track variables in a room using the following methods:
client.saveVariable({ name: string, room: string, value: unknown }): Promise<void>
client.readVariable({ name: string, room: string }): Promise<Value>
client.listenVariable({ name: string, room: string }): AsyncIterable<Value>
[!WARNING]
readVariable
and listenVariable
return a Value
object. To get the underlying value, you must call the Value.unwrap
function.
This is because the functions can return nothing due to an error, and the Value
object allows you to check if the value is an error or not.
Example
import { createRoomApiClient } from "@workadventure/room-api-client";
const client = createRoomApiClient("My AWESOME KEY");
const roomUrl = "https://play.workadventu.re/@/my-organization/my-world/my-room";
const variableName = "textField";
async function init() {
await client.saveVariable({
name: variableName,
room: roomUrl,
value: "Default Value",
});
console.log("Value saved: Default Value");
const value = await client.readVariable({
name: variableName,
room: roomUrl,
});
console.log("Value read:", Value.unwrap(value));
setTimeout(async () => {
await client.saveVariable({
name: variableName,
room: roomUrl,
value: "New Value",
});
console.log("Value saved: New Value");
}, 5000);
const listenVariable = client.listenVariable({
name: variableName,
room: roomUrl,
});
for await (const value of listenVariable) {
console.log("Value listened:", Value.unwrap(value));
break;
}
}
init();
Sending events / listening to events
The Room API client allows you to send and listen to events in a room using the following methods:
client.broadcastEvent({ name: string, room: string, data: unknown }): Promise<void>
client.listenToEvent({ name: string, room: string }): AsyncIterable<any>
Example
import { createRoomApiClient } from "@workadventure/room-api-client";
const client = createRoomApiClient("My AWESOME KEY");
const roomUrl = "https://play.workadventu.re/@/my-organization/my-world/my-room";
const eventName = "my-event";
async function init() {
setTimeout(async () => {
await client.broadcastEvent({
name: eventName,
room: roomUrl,
data: "Default Value",
});
console.log("Event sent: Default Value");
}, 5000);
const events = client.listenToEvent({
name: eventName,
room: roomUrl,
});
for await (const event of events) {
console.log("Event received:");
console.log("Sender:", event.senderId);
console.log("Value:", event.data);
break;
}
}
init();