use-truapi
Chat

useChatRoom

Register a chat room with the host, idempotently

useChatRoom registers a chat room your product hosts. data resolves to "New" (the room was just created) or "Exists" (it was already registered). Registration is idempotent: the result is cached per roomId for the runtime's lifetime, so mounting the hook from several components performs a single host call, and re-registering an existing room is harmless anyway, it just resolves "Exists".

Chat is host-only. Running standalone (outside a Polkadot host) the query result settles in the error state with HostUnavailableError; gate chat UI with useIsHost, or render the error as a fallback.

Usage

import { useChatRoom } from "@use-truapi/react";

const room = {
  roomId: "support",
  name: "Support",
  icon: "https://example.com/support.png",
};

function SupportRoom() {
  const { data: status, isPending, error } = useChatRoom(room);

  if (isPending) return <p>Opening room…</p>;
  if (error) return <p>Chat unavailable: {error.message}</p>;

  return <p>Room ready ({status === "New" ? "just created" : "already existed"}).</p>;
}
<script setup lang="ts">
import { useChatRoom } from "@use-truapi/vue";

const room = {
  roomId: "support",
  name: "Support",
  icon: "https://example.com/support.png",
};

const { data: status, isPending, error } = useChatRoom(room);
</script>

<template>
  <p v-if="isPending">Opening room…</p>
  <p v-else-if="error">Chat unavailable: {{ error.message }}</p>
  <p v-else>Room ready ({{ status === "New" ? "just created" : "already existed" }}).</p>
</template>

Once registration has settled, wire up useChatMessages and useSendChatMessage against the same roomId for a working chat panel.

See also

  • useChatBot registers a bot identity for posting into rooms.
  • useChatRooms lists the rooms the product participates in, live.

API reference

React

function useChatRoom(room, options?): UseQueryResult<"New" | "Exists", Error>;

Register a chat room (idempotent). Chat is host-only.

Parameters

ParameterType
roomChatRoomDefinition
options?{ query?: QueryOptions<"New" | "Exists">; }
options.query?QueryOptions<"New" | "Exists">

Returns

UseQueryResult<"New" | "Exists", Error>

Vue

function useChatRoom(room, options?): QueryResult<"New" | "Exists">;

Register a chat room (idempotent). Chat is host-only.

Parameters

ParameterType
roomChatRoomDefinition
options?{ query?: QueryOptions<"New" | "Exists">; }
options.query?QueryOptions<"New" | "Exists">

Returns

QueryResult<"New" | "Exists">

On this page