Skip to content

Instantly share code, notes, and snippets.

@hieptl
Last active October 27, 2021 05:39
Show Gist options
  • Save hieptl/24d05fb82f2b8b1fcf3a692806e4d26c to your computer and use it in GitHub Desktop.
Save hieptl/24d05fb82f2b8b1fcf3a692806e4d26c to your computer and use it in GitHub Desktop.
Chat.tsx - Listen for Online/Offline Users - Ionic Chat App
...
const Chat: React.FC = () => {
...
const { cometChat, user, selectedConversation, setCallType } = useContext(Context);
...
useEffect(() => {
...
return () => {
if (selectedConversation) {
...
if (selectedConversation.contactType === 0) {
cometChat.removeUserListener(userOnlineListenerId);
}
...
}
}
}, [selectedConversation]);
...
const listenForOnlineUsers = () => {
cometChat.addUserListener(
userOnlineListenerId,
new cometChat.UserListener({
onUserOnline: (onlineUser: any) => {
if (onlineUser && onlineUser.uid === selectedConversation.uid) {
setIsUserOnline(() => true);
}
},
onUserOffline: (offlineUser: any) => {
if (offlineUser && offlineUser.uid === selectedConversation.uid) {
setIsUserOnline(() => false);
}
}
})
);
};
};
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment