BarrageState module from the AtomicXCore framework.
BarrageState delivers a complete solution for live comments in your live streaming app, including:BarrageState instance tied to the current live room's liveID. Subscribe to messageList to receive the latest complete list of live comment messages.import { useEffect } from 'react';import { useBarrageState } from 'react-native-tuikit-atomic-x/lib/module/atomic-x/state/BarrageState';// Get BarrageState instance for the given liveIDconst { messageList } = useBarrageState(liveID);// Subscribe to real-time updates and trigger UI changesuseEffect(() => {console.log(messageList);}, [messageList]);
sendTextMessage method to broadcast a plain text message to all users in the live room.import { useBarrageState } from 'react-native-tuikit-atomic-x/lib/module/atomic-x/state/BarrageState';// Get BarrageState instance for the given liveIDconst { sendTextMessage } = useBarrageState(liveID);const sendMessage = () => {sendTextMessage({liveID: liveID, // Current live room's liveIDtext: 'xxx', // The live comment you want to sendonSuccess: () => {console.log('Live comment sent successfully');},onError: (error) => {console.log('Failed to send live comment', error);},});};
Parameter | Type | Description |
liveID | string | The liveID of the current live room. |
text | string | The text content to send. |
onSuccess | Function | Callback for successful send. |
onError | Function | Callback for failed send. |
import { useBarrageState } from 'react-native-tuikit-atomic-x/lib/module/atomic-x/state/BarrageState';// Get BarrageState instance for the given liveIDconst { sendCustomMessage } = useBarrageState(liveID);// Send a custom live comment, e.g., for sending giftsconst sendGiftMessage = (giftID, giftCount) => {// 1. Define a business-recognizable IDconst businessID = 'live_gift';// 2. Encode business data as a JSON stringconst giftData = { gift_id: giftID, gift_count: giftCount };// 3. Call the core API to send the custom messagesendCustomMessage({liveID: liveID, // Current live room's liveIDbusinessID,data: JSON.stringify(giftData),onSuccess: () => {console.log('Gift message sent successfully');},onError: (error) => {console.log('Failed to send gift message', error);},});};
Parameter | Type | Description |
liveID | string | The liveID of the current live room. |
businessID | string | Unique business identifier (e.g., "live_gift") for distinguishing custom messages. |
data | string | Business data, typically as a JSON string. |
onSuccess | Function | Callback for successful send. |
onError | Function | Callback for failed send. |
import { useBarrageState } from 'react-native-tuikit-atomic-x/lib/module/atomic-x/state/BarrageState';// Get BarrageState instance for the given liveIDconst { appendLocalTip } = useBarrageState(liveID);const showWelcomeMessage = (user) => {appendLocalTip({liveID: liveID, // Current live room's liveIDmessage: {textContent: `Welcome ${user.userName} to the live room!`,sender: {userID: user.userID || '',userName: user.userName || '',avatarURL: user.avatarURL || '',},messageType: 0,sequence: Math.floor(Date.now() / 1000),timestampInSecond: Math.floor(Date.now() / 1000),liveID: liveID,},onSuccess: () => {console.log('Local message inserted successfully');},onError: (error) => {console.log('Failed to insert local message', error);},});};
Parameter | Type | Description |
liveID | string | The liveID of the current voice chat room. |
message | object | The message object to insert locally. The SDK appends this object to messageList. |
onSuccess | Function | Callback for successful local message addition. |
onError | Function | Callback for failed local message addition. |
disableSendMessage interface in LiveAudienceState to mute or unmute a specific user. This state persists—even if the user rejoins the live room, the mute remains in effect.import { useLiveAudienceState } from 'react-native-tuikit-atomic-x/lib/module/atomic-x/state/LiveAudienceState';// Get LiveAudienceState instance for the given liveIDconst { disableSendMessage } = useLiveAudienceState(liveID);// Define the user ID and mute statusconst userIdToMute = 'user_id_to_be_muted'; // UserID to operate onconst shouldDisable = true; // true to mute, false to unmute// Execute mute/unmute operationconst toggleMuteUser = () => {disableSendMessage({liveID: liveID, // Current live room liveIDuserID: userIdToMute,isDisable: shouldDisable,onSuccess: () => {console.log(`${shouldDisable ? 'Muted' : 'Unmuted'} user ${userIdToMute} successfully`);},onError: (error) => {console.log('Operation failed', error);},});};
Parameter | Type | Description |
liveID | string | The liveID of the current voice chat room. |
userID | string | The userID to operate. |
isDisable | boolean | Whether to mute the user. |
onSuccess | Function | Callback for successful mute. |
onError | Function | Callback for failed mute. |
updateLiveInfo in LiveListState.import { useLiveListState } from 'react-native-tuikit-atomic-x/lib/module/atomic-x/state/LiveListState';import { LiveInfoModifyFlag } from 'react-native-tuikit-atomic-x/lib/module/atomic-x/state/LiveListState/types';// 1. Get LiveListState instanceconst { updateLiveInfo, currentLive } = useLiveListState();// 2. Get current live room info and change mute-all statusconst isCurrentlyMuted = currentLive?.isMessageDisable || false;// 3. Call update interface and specify modify flagconst toggleMuteAll = () => {updateLiveInfo({liveInfo: { liveID: currentLive?.liveID, isMessageDisable: !isCurrentlyMuted },modifyFlagList: [LiveInfoModifyFlag.IS_MESSAGE_DISABLE],onSuccess: () => {console.log(`${!isCurrentlyMuted ? 'All users muted' : 'Mute cancelled'} successfully`);},onError: (error) => {console.log('Operation failed', error);},});};
Parameter | Type | Description |
liveInfo | object | liveID required; other fields specify live status updates. |
modifyFlagList | LiveInfoModifyFlag[] | List of live info flags to update. |
onSuccess | Function | Callback for successful live info update. |
onError | Function | Callback for failed live info update. |
BarrageState, use the following strategies to ensure smooth and stable performance—even during high-traffic live streaming events. This section provides actionable solutions and sample code for key scenarios.import { useEffect, useRef, useState } from 'react';import { useBarrageState } from 'react-native-tuikit-atomic-x/lib/module/atomic-x/state/BarrageState';// Get BarrageState instance for liveIDconst { messageList } = useBarrageState(liveID);// UI display dataconst [displayMessageList, setDisplayMessageList] = useState([]);// ===== Throttle configuration =====const THROTTLE_INTERVAL = 300; // 300ms throttle thresholdconst latestMessageListRef = useRef([]);const throttleTimerRef = useRef(null);const lastFlushTimeRef = useRef(0);useEffect(() => {if (!messageList) return;// Cache the latest full list without triggering renderlatestMessageListRef.current = messageList;const now = Date.now();const timeSinceLastFlush = now - lastFlushTimeRef.current;// If enough time has passed, update immediatelyif (timeSinceLastFlush >= THROTTLE_INTERVAL) {setDisplayMessageList([...latestMessageListRef.current]);lastFlushTimeRef.current = now;} else if (!throttleTimerRef.current) {// Set timer only if not already setconst remainingTime = THROTTLE_INTERVAL - timeSinceLastFlush;throttleTimerRef.current = setTimeout(() => {setDisplayMessageList([...latestMessageListRef.current]);lastFlushTimeRef.current = Date.now();throttleTimerRef.current = null;}, remainingTime);}// Intermediate updates within 300ms are skipped; timer flushes with latest data}, [messageList]);// Clear timer on component unmountuseEffect(() => {return () => {if (throttleTimerRef.current) {clearTimeout(throttleTimerRef.current);}};}, []);// Use displayMessageList to update UI, e.g.:// <FlatList data={displayMessageList} ... />
messageList grows indefinitely over time. Even with UI throttling, holding a large array increases memory usage and can eventually cause crashes.import { useEffect, useState } from 'react';import { useBarrageState } from 'react-native-tuikit-atomic-x/lib/module/atomic-x/state/BarrageState';// Get BarrageState instance for liveIDconst { messageList } = useBarrageState(liveID);// UI display dataconst [displayMessageList, setDisplayMessageList] = useState([]);// ===== Message window configuration (retain latest N messages) =====const MAX_MESSAGES_COUNT = 500;const trimMessageList = (messages) => {if (messages.length > MAX_MESSAGES_COUNT) {console.warn(`[BarrageList] Message count exceeds limit ${MAX_MESSAGES_COUNT}, deleted ${messages.length - MAX_MESSAGES_COUNT} oldest messages`);return messages.slice(messages.length - MAX_MESSAGES_COUNT);}return messages;};// Listen for messageList changes, trim, and update UIuseEffect(() => {if (!messageList) return;setDisplayMessageList(trimMessageList([...messageList]));}, [messageList]);// Use displayMessageList to update UI, e.g.:// <FlatList data={displayMessageList} ... />
sendCustomMessage. BarrageState does not restrict your business logic or creativity.{ "type": "colored_text", "text": "This is a colored live comment!", "color": "#FF5733" }
data parameter of sendCustomMessage. Set businessID to a unique identifier for your business, such as barrage_style_v1.messageType 'CUSTOM' and matching businessID. Parse the data string (typically JSON), and render your UI based on the parsed information (e.g., color, text).sendTextMessage succeeded or failed. If it failed, the error message will clarify the reason (e.g., "You have been muted," "Network error").barrageState after joining the live room with the appropriate liveID. Subscribing before joining may cause you to miss messages.liveID used for creating BarrageState, joining the live room, and sending messages is exactly the same—including case sensitivity.AtomicXCore supports retrieving historical live comment messages, but you need to enable this in the server console. Once configured, the SDK handles everything automatically—no extra client code is required.
AtomicXCore automatically retrieves the configured number of historical chat messages in the background. These messages are delivered to the UI layer through the BarrageState subscription channel, just like real-time messages. Your application will receive and display these historical chat messages in the same way as live messages.Was this page helpful?
You can also Contact sales or Submit a Ticket for help.
Help us improve! Rate your documentation experience in 5 mins.
Feedback