| 
 | 1 | +---  | 
 | 2 | +id: channel-user-lists  | 
 | 3 | +sidebar_position: 21  | 
 | 4 | +title: Channel Members and Online Status  | 
 | 5 | +---  | 
 | 6 | + | 
 | 7 | +In this example, we will demonstrate how to render the current channel members and their online status.  | 
 | 8 | + | 
 | 9 | +## Render the Channel Members List  | 
 | 10 | + | 
 | 11 | +Let's start the example by creating a simple members list component. To access the members list of the current channel, we will get the current channel using `useChannelStateContext` hook.   | 
 | 12 | +The example is a bit more convoluted, since we will add online presence updates at the next step.  | 
 | 13 | + | 
 | 14 | +:::note  | 
 | 15 | +In order for the client to receive updates for user presence, ensure that you are watching the channel with `channel.watch({ presence: true })`. More details can be found in the [↗ LLC documentation](https://getstream.io/chat/docs/javascript/watch_channel/?language=javascript).  | 
 | 16 | +:::  | 
 | 17 | + | 
 | 18 | +```tsx  | 
 | 19 | +const Users = () => {  | 
 | 20 | +  const { channel } = useChannelStateContext();  | 
 | 21 | +  const [channelUsers, setChannelUsers] = useState<  | 
 | 22 | +    Array<{ name: string; online: boolean }>  | 
 | 23 | +  >([]);  | 
 | 24 | + | 
 | 25 | +  useEffect(() => {  | 
 | 26 | +    const updateChannelUsers = () => {  | 
 | 27 | +      setChannelUsers(  | 
 | 28 | +        Object.values(channel.state.members).map((user) => ({  | 
 | 29 | +          name: user.user_id!,  | 
 | 30 | +          online: !!user.user!.online,  | 
 | 31 | +        }))  | 
 | 32 | +      );  | 
 | 33 | +    };  | 
 | 34 | + | 
 | 35 | +    updateChannelUsers();  | 
 | 36 | +  }, [client, channel]);  | 
 | 37 | + | 
 | 38 | +  return (  | 
 | 39 | +    <ul className="users-list">  | 
 | 40 | +      {channelUsers.map((member) => (  | 
 | 41 | +        <li key={member.name}>  | 
 | 42 | +          {member.name} - {member.online ? "online" : "offline"}  | 
 | 43 | +        </li>  | 
 | 44 | +      ))}  | 
 | 45 | +    </ul>  | 
 | 46 | +  );  | 
 | 47 | +};  | 
 | 48 | +```  | 
 | 49 | + | 
 | 50 | +We can put the component as a child of the `Channel` component:  | 
 | 51 | + | 
 | 52 | +```tsx  | 
 | 53 | +  <Channel>  | 
 | 54 | +    <Window>  | 
 | 55 | +      <Users />  | 
 | 56 | +      <ChannelHeader />  | 
 | 57 | +      <MessageList />  | 
 | 58 | +      <MessageInput focus />  | 
 | 59 | +    </Window>  | 
 | 60 | +    <Thread />  | 
 | 61 | +  </Channel>  | 
 | 62 | +```  | 
 | 63 | + | 
 | 64 | +## Real-Time Updates   | 
 | 65 | + | 
 | 66 | +So far, our list looks good, but there's a catch: for performance purposes, the `useChannelStateContext` does not refresh when user presence changes.   | 
 | 67 | +To make the list refresh accordingly, we need to attach an additional listener to the `user.presence.changed` event of the chat client.   | 
 | 68 | +Let's also add some basic CSS to complete the look of the list. A class is already applied to the JSX, just add a CSS file and be sure to import into your file.  | 
 | 69 | + | 
 | 70 | +```css  | 
 | 71 | +.users-list {  | 
 | 72 | +  background: #ffffff;  | 
 | 73 | +  padding: 20px;  | 
 | 74 | +  padding-left: 30px;  | 
 | 75 | +  border-radius: calc(16px / 2) 16px 0 0;  | 
 | 76 | +  border: 1px solid #ecebeb;  | 
 | 77 | +}  | 
 | 78 | +```  | 
 | 79 | + | 
 | 80 | +```tsx  | 
 | 81 | +const Users = () => {  | 
 | 82 | +  const { client } = useChatContext();  | 
 | 83 | +  const { channel } = useChannelStateContext();  | 
 | 84 | +  const [channelUsers, setChannelUsers] = useState<  | 
 | 85 | +    Array<{ name: string; online: boolean }>  | 
 | 86 | +  >([]);  | 
 | 87 | +  useEffect(() => {  | 
 | 88 | +    const updateChannelUsers = (event?: Event) => {  | 
 | 89 | +      // test if the updated user is a member of this channel  | 
 | 90 | +      if (!event || channel.state.members[event.user!.id] !== undefined) {  | 
 | 91 | +        setChannelUsers(  | 
 | 92 | +          Object.values(channel.state.members).map((user) => ({  | 
 | 93 | +            name: user.user_id!,  | 
 | 94 | +            online: !!user.user!.online,  | 
 | 95 | +          }))  | 
 | 96 | +        );  | 
 | 97 | +      }  | 
 | 98 | +    };  | 
 | 99 | + | 
 | 100 | +    updateChannelUsers();  | 
 | 101 | + | 
 | 102 | +    //   | 
 | 103 | +    client.on("user.presence.changed", updateChannelUsers);  | 
 | 104 | + | 
 | 105 | +    return () => {  | 
 | 106 | +      client.off("user.presence.changed", updateChannelUsers);  | 
 | 107 | +    };  | 
 | 108 | +  }, [client, channel]);  | 
 | 109 | + | 
 | 110 | +  return (  | 
 | 111 | +    <ul className="users-list">  | 
 | 112 | +      {channelUsers.map((member) => (  | 
 | 113 | +        <li key={member.name}>  | 
 | 114 | +          {member.name} - {member.online ? "online" : "offline"}  | 
 | 115 | +        </li>  | 
 | 116 | +      ))}  | 
 | 117 | +    </ul>  | 
 | 118 | +  );  | 
 | 119 | +};  | 
 | 120 | +```  | 
 | 121 | + | 
 | 122 | +With the above addition, `channelUsers` will be updated each time user comes online or goes offline.  | 
 | 123 | + | 
0 commit comments