What is a “Room”?

In the context of the Kwei Client SDK, a “room” represents a virtual space where real-time communication occurs. This can include audio and video streams, data transfer, and more, allowing multiple participants to interact as if they were in the same physical room. Rooms are essential constructs in applications that require collaborative features such as video conferences, live chats, and interactive sessions.

Initializing the SDK

Before you can create or join a room, the SDK must be properly initialized. This initialization process involves setting up with your unique API keys and configuring any necessary parameters that tailor the SDK to your application’s needs. This process is asynchronous, meaning it might complete at some point in the future without blocking the execution of other scripts.

const kwei = require("kwei-client-js-sdk");
const room = new kwei.Room();

// Initialize asynchronously
room
  .init("your_api_key", "your_secret_key")
  .then(() => {
    console.log("SDK successfully initialized");
  })
  .catch((error) => {
    console.error("Initialization failed:", error);
  });

Joining a Room

Once the SDK is initialized, you can proceed to join a room. It is crucial to ensure that the initialization has completed before attempting to join a room, as doing so prematurely can result in errors.

Asynchronous Initialization

The init method returns a promise, which resolves when the initialization is complete. You can use await within an async function, or continue in a .then() callback to handle this correctly:

// Using async/await
async function setupRoom() {
  try {
    await room.init("your_api_key", "your_secret_key");
    console.log("Initialization successful");
    await room.joinRoom("ExampleRoom");
    console.log("Joined room successfully");
  } catch (error) {
    console.error("Error during setup:", error);
  }
}

// Using .then()
room
  .init("your_api_key", "your_secret_key")
  .then(() => {
    room
      .joinRoom("ExampleRoom")
      .then(() => {
        console.log("Joined room successfully");
      })
      .catch((error) => {
        console.error("Error joining room:", error);
      });
  })
  .catch((error) => {
    console.error("Initialization failed:", error);
  });

Providing a Room Name

The room name acts as an identifier and a means for users to connect to the same communication session. When calling joinRoom, specify the name of the room you want to join or create. If the room does not exist, it will be created automatically, allowing other users to join thereafter using the same room name.

Best Practices for Room Names

  • Uniqueness: Choose a unique name that can be easily communicated and remembered by participants.
  • Security: Avoid using sensitive information as part of the room name.
  • Consistency: Use a consistent naming convention that can scale with your application.

Conclusion

Rooms are a fundamental part of the Kwei SDK, enabling real-time interaction among users. Proper initialization of the SDK is crucial before any room operations can be performed to ensure that everything functions smoothly and securely. Follow the outlined steps to ensure that your integration of the Kwei SDK supports robust real-time communication features effectively.