npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@workadventure/room-api-client

v1.22.2

Published

Workadventure Room Api Client

Downloads

230

Readme

@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";

/**
 * By default, the client targets the official WorkAdventure server,
 * but you can also define customs domain and port.
 * Example :
 * const client = createRoomApiClient("My AWESOME KEY", "mydomain.net", "5221");
 */
const client = createRoomApiClient("My AWESOME KEY");

// URL of the room you wish to interact with
const roomUrl = "https://play.workadventu.re/@/my-organization/my-world/my-room";

// Name of the variable with which you want to interact
const variableName = "textField";

async function init() {
  // Save a variable
  await client.saveVariable({
    name: variableName,
    room: roomUrl,
    value: "Default Value",
  });

  console.log("Value saved: Default Value");

  // Read a variable
  const value = await client.readVariable({
    name: variableName,
    room: roomUrl,
  });

  console.log("Value read:", Value.unwrap(value));

  // Save a variable in 5sec
  setTimeout(async () => {
    await client.saveVariable({
      name: variableName,
      room: roomUrl,
      value: "New Value",
    });

    console.log("Value saved: New Value");
  }, 5000);

  // Listen a variable
  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";

/**
 * By default, the client targets the official WorkAdventure server,
 * but you can also define customs domain and port.
 * Example :
 * const client = createRoomApiClient("My AWESOME KEY", "mydomain.net", "5221");
 */
const client = createRoomApiClient("My AWESOME KEY");

// URL of the room you wish to interact with
const roomUrl = "https://play.workadventu.re/@/my-organization/my-world/my-room";

// Name of the event with which you want to interact
const eventName = "my-event";

async function init() {
  // Send an event in 5 seconds
  setTimeout(async () => {
    await client.broadcastEvent({
      name: eventName,
      room: roomUrl,
      data: "Default Value",
    });

    console.log("Event sent: Default Value");
  }, 5000);

  // Listen a event
  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();