use-truapi
Statements

usePublishStatement

Publish small JSON payloads to the app topic

usePublishStatement publishes ephemeral JSON payloads to your app's statement-store topic. Call publish(data, options?) with any JSON-serializable value; subscribers on the same topic (see useStatements) receive it within moments. Mutation state (data, error, isPending, reset) tracks the last publish.

The resolved boolean tells host from standalone: true means the host's statement store accepted the statement, false means the app runs standalone and there is nothing to publish to. A host-side rejection rejects with the SDK's StatementStoreError and lands in error: StatementDataTooLargeError when the JSON encoding exceeds the 512 byte limit, StatementConnectionError when the store is not connected, StatementSubmitError when the host refused to sign or the network refused the statement (for example a missing statement-store allowance).

Usage

A publish and subscribe round trip: every mounted PingBoard sees every ping, including its own.

import { usePublishStatement, useStatements } from "@use-truapi/react";

interface Ping {
  from: string;
  sentAt: number;
}

function PingBoard({ name }: { name: string }) {
  const { data: pings } = useStatements<Ping>();
  const { publish, isPending, error } = usePublishStatement<Ping>();

  async function onPing() {
    const delivered = await publish({ from: name, sentAt: Date.now() });
    if (!delivered) console.warn("ping not delivered (rejected or standalone)");
  }

  return (
    <>
      <button type="button" onClick={onPing} disabled={isPending}>
        Ping everyone
      </button>
      {error && <p role="alert">{error.message}</p>}
      <ul>
        {pings?.map((p, i) => (
          <li key={i}>
            {p.data.from} pinged at {new Date(p.data.sentAt).toLocaleTimeString()}
          </li>
        ))}
      </ul>
    </>
  );
}
<script setup lang="ts">
import { usePublishStatement, useStatements } from "@use-truapi/vue";

interface Ping {
  from: string;
  sentAt: number;
}

const props = defineProps<{ name: string }>();

const { data: pings } = useStatements<Ping>();
const { publish, isPending, error } = usePublishStatement<Ping>();

async function onPing() {
  const delivered = await publish({ from: props.name, sentAt: Date.now() });
  if (!delivered) console.warn("ping not delivered (rejected or standalone)");
}
</script>

<template>
  <button type="button" :disabled="isPending" @click="onPing">
    Ping everyone
  </button>
  <p v-if="error" role="alert">{{ error.message }}</p>
  <ul>
    <li v-for="(p, i) in pings" :key="i">
      {{ p.data.from }} pinged at {{ new Date(p.data.sentAt).toLocaleTimeString() }}
    </li>
  </ul>
</template>

The second argument forwards PublishOptions to the store: scope with topic2 (subscribers only see it if they filter on the same value) or override the time-to-live with ttlSeconds:

await publish(
  { from: "alice", sentAt: Date.now() },
  { topic2: "room-42", ttlSeconds: 60 },
);

In a fire-and-forget handler, void publish(data).catch(() => {}) is enough; failures also land in error.

See also

API reference

React

function usePublishStatement<T>(options?): NamedMutation<boolean, PublishStatementVariables<T>> & object;

Publish JSON payloads (≤512 bytes) to the app topic: publish(data). Resolves false standalone (nothing to publish to) and true once the host accepted the statement; a host-side rejection rejects with the SDK's StatementStoreError so the reason lands in error.

Type Parameters

Type ParameterDefault type
Tunknown

Parameters

ParameterType
options?{ mutation?: MutationOptions<boolean, PublishStatementVariables<T>>; }
options.mutation?MutationOptions<boolean, PublishStatementVariables<T>>

Returns

NamedMutation<boolean, PublishStatementVariables<T>> & object

Type Parameters

Type Parameter
T

Properties

data

data: T;

options?

optional options?: PublishOptions;

Vue

function usePublishStatement<T>(options?): NamedMutation<boolean, PublishStatementVariables<T>> & object;

Publish JSON payloads (≤512 bytes) to the app topic: publish(data). Resolves false standalone (nothing to publish to) and true once the host accepted the statement; a host-side rejection rejects with the SDK's StatementStoreError so the reason lands in error.

Type Parameters

Type ParameterDefault type
Tunknown

Parameters

ParameterType
options?{ mutation?: MutationOptions<boolean, PublishStatementVariables<T>>; }
options.mutation?MutationOptions<boolean, PublishStatementVariables<T>>

Returns

NamedMutation<boolean, PublishStatementVariables<T>> & object

Type Parameters

Type Parameter
T

Properties

data

data: T;

options?

optional options?: PublishOptions;

On this page