LiveAudienceState is a dedicated module in AtomicXCore designed for managing audience information in live streaming rooms. With LiveAudienceState, you can build a robust audience list and management system for your live streaming application.
LiveAudienceState instance tied to the current live room's liveID, and proactively fetch the audience list for initial UI rendering.import { useEffect } from 'react';import { useLiveAudienceState } from 'react-native-tuikit-atomic-x/lib/module/atomic-x/state/LiveAudienceState';const liveID = 'xxx'; // The liveID of the current voice chat room or live streaming room// Get the LiveAudienceState instance by liveIDconst { fetchAudienceList, audienceList } = useLiveAudienceState(liveID);useEffect(() => {fetchAudienceList({liveID: liveID, // The liveID of the current live roomonSuccess: () => {// Upon success, the reactive data will automatically updateconsole.log('Successfully fetched the audience list for the first time');},onError: (error) => {console.log('Failed to fetch the audience list for the first time', error);},});}, []);// Listen for real-time changes in audienceList to drive UI updatesuseEffect(() => {console.log(audienceList);}, [audienceList]);
LiveAudienceState events and reactive data to track full audience snapshots and real-time join/leave events for UI updates.import { useEffect } from 'react';import { useLiveAudienceState } from 'react-native-tuikit-atomic-x/lib/module/atomic-x/state/LiveAudienceState';const liveID = 'xxx'; // The liveID of the current voice chat room or live streaming room// Get the LiveAudienceState instance by liveIDconst { audienceList, audienceCount, addAudienceListener, removeAudienceListener } = useLiveAudienceState(liveID);// Listen for real-time changes in audienceList to drive UI updatesuseEffect(() => {console.log(audienceList);}, [audienceList]);// Listen for real-time changes in audienceCount to drive UI updatesuseEffect(() => {console.log(audienceCount);}, [audienceCount]);// Subscribe to audience join/leave eventsuseEffect(() => {const onAudienceJoined = (event) => {const newAudience = JSON.parse(event);console.log(`Audience member ${newAudience.userName} joined the live room`);};const onAudienceLeft = (event) => {const departedAudience = JSON.parse(event);console.log(`Audience member ${departedAudience.userName} left the live room`);};addAudienceListener('onAudienceJoined', onAudienceJoined);addAudienceListener('onAudienceLeft', onAudienceLeft);// Clean up listeners when the component unmountsreturn () => {removeAudienceListener('onAudienceJoined', onAudienceJoined);removeAudienceListener('onAudienceLeft', onAudienceLeft);};}, []);
kickUserOutOfRoom API to remove a specific user from the live room.import { useLiveAudienceState } from 'react-native-tuikit-atomic-x/lib/module/atomic-x/state/LiveAudienceState';const liveID = 'xxx'; // The liveID of the current voice chat room or live streaming room// Get the LiveAudienceState instance by liveIDconst { kickUserOutOfRoom } = useLiveAudienceState(liveID);const kickOut = () => {kickUserOutOfRoom({liveID: 'xxx', // The liveID of the current live roomuserID: userIDToKick, // The userID of the member to be removedonSuccess: () => {console.log(`Successfully removed user ${userID} from the room`);// After successful removal, you will receive the onAudienceLeft event},onError: (error) => {console.log(`Failed to remove user ${userID} from the room`, error);},});};
setAdministrator and revokeAdministrator APIs.import { useLiveAudienceState } from 'react-native-tuikit-atomic-x/lib/module/atomic-x/state/LiveAudienceState';const liveID = 'xxx'; // The liveID of the current voice chat room or live streaming room// Get the LiveAudienceState instance by liveIDconst { setAdministrator, revokeAdministrator } = useLiveAudienceState(liveID);// Promote a user to administratorconst promoteToAdmin = () => {setAdministrator({liveID: 'xxx', // The liveID of the current live roomuserID: userIDToAdmin, // The userID of the member to be promoted to administratoronSuccess: () => {console.log(`Successfully promoted user ${userID} to administrator`);},onError: (error) => {console.log(`Failed to promote user ${userID} to administrator`, error);},});};// Revoke a user's administrator statusconst revokeAdmin = () => {revokeAdministrator({liveID: 'xxx', // The liveID of the current live roomuserID: userIDRevokeAdmin, // The userID of the administrator whose status will be revokedonSuccess: () => {console.log(`Successfully revoked administrator status for user ${userID}`);},onError: (error) => {console.log(`Failed to revoke administrator status for user ${userID}`, error);},});};
'onAudienceJoined' event from LiveAudienceState to receive real-time notifications when new audience members join. Extract the new member's nickname and immediately call the appendLocalTip API from BarrageState to display the message in the live comments.import { useEffect } from 'react';import { useLiveAudienceState } from 'react-native-tuikit-atomic-x/lib/module/atomic-x/state/LiveAudienceState';import { useBarrageState } from 'react-native-tuikit-atomic-x/lib/module/atomic-x/state/BarrageState';const liveID = 'xxx'; // The liveID of the current voice chat room or live streaming room// Get the LiveAudienceState instance by liveIDconst { addAudienceListener } = useLiveAudienceState(liveID);// Get the BarrageState instance by liveIDconst { appendLocalTip } = useBarrageState(liveID);useEffect(() => {addAudienceListener('onAudienceJoined', (event) => {const parsed = typeof event === 'string' ? JSON.parse(event) : event;const audienceData = typeof parsed.audience === 'string' ? JSON.parse(parsed.audience) : (parsed.audience || parsed);const name = audienceData.userName || audienceData.nickname || audienceData.userID || 'unknown';// Create a local tip message (according to SDK's actual message structure)const welcomeTip = {textContent: `Welcome ${name} to the live room!`,sender: {userID: audienceData.userID || '',userName: name,avatarURL: audienceData.avatarURL || '',},messageType: 0,sequence: Math.floor(Date.now() / 1000),timestampInSecond: Math.floor(Date.now() / 1000),liveID: liveID,};// Call BarrageStore API to insert it into the live comments listappendLocalTip({liveID,message: welcomeTip,onSuccess: () => {console.log(`Audience member ${name} joined the live room`);},onError: (error) => {console.log('Failed to insert live comment', error);},});});}, []);
State | Description | API Documentation |
LiveAudienceState | Audience management: Retrieve real-time audience list (ID / Name / Avatar), track audience count, monitor join/leave events. | |
BarrageState | Live comments: Send text/custom comments, maintain comment list, monitor comment status in real time. |
audienceCount) updated in LiveAudienceState? audienceCount in LiveAudienceState immediately.audienceCount provides a highly accurate, near real-time estimate. However, in high-concurrency scenarios, you may see brief delays or occasional data loss. We recommend using audienceCount for UI display purposes only, not as the sole source for billing, statistics, or other scenarios requiring absolute accuracy.피드백