Skip to main content

On Channel Ready

onChannelReady fires once the chat channel is actually ready to accept messages. Use it to send an initial message right after the chatbot mounts (e.g. seeding a new conversation with a SQL statement) — no more polling to detect when sendMessage becomes available.

The callback re-fires whenever the channel is replaced (e.g. after a manual resetChannel). Use a ref guard in the consumer if the work should only happen once.

Settings

Ready events (0)

Waiting for first onChannelReady...

Verification

Each event records whether ref.current.serviceContext.sendMessage was a function at the moment the callback fired. Both flags should be true on every fire.

Loading chatbot...

Code Example

import { Chatbot, ChatbotRef } from "@asgard-js/react";
import { useCallback, useRef } from "react";

function MyChatbot({ initialText }: { initialText?: string }) {
const chatbotRef = useRef<ChatbotRef>(null);

const handleChannelReady = useCallback(() => {
if (initialText) {
chatbotRef.current?.serviceContext?.sendMessage?.({
text: initialText,
blobIds: [],
});
}
}, [initialText]);

return (
<Chatbot
ref={chatbotRef}
config={{ botProviderEndpoint: "..." }}
customChannelId="my-channel"
onChannelReady={handleChannelReady}
/>
);
}

Behavior

  • Fires once after the channel is created and the imperative ref has been updated
  • Re-fires after a manual resetChannel
  • Inside the callback, ref.current.serviceContext.sendMessage is bound to the new channel