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 is data, not an exception: true means the host's statement store accepted the statement, false means it was undeliverable (the store rejected it, or the app runs standalone). Thrown errors are reserved for real failures, such as a payload whose JSON encoding exceeds the 512 byte statement limit.

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 when the store rejects the statement or the app runs standalone.

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 when the store rejects the statement or the app runs standalone.

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