Twilio Chat: Trying to get notification of Member Reachability on a Channel

Viewed 321

I'm trying get notifications to other members of a channel when a member of a chat channel has left the room. Specifically if they navigate away from the page in their browser. I have "Reachability Enabled" on the service, and am getting verification of that by checking the Client.reachabilityEnabled member. I'm able to access a list of all of the members of the channel by calling on Channel.getMembers(), but the userInfoUpdated event does not fire when a member enters or leaves the chat page.

A side, but possibly relevant item is that the member.state.attributes object is empty when I inspect any member in the console.(reference this question Twilio chat member online status is always null -- it shows an image of the console inspector that has values including online status in the member.state.attributes object) -

Running the code below, I get my notification that Reachability is enabled and my console log of the members, but when I have some other member enter/exit the page no event fires.

/*twilioChat is the return from  require: https://media.twiliocdn.com/sdk/js/chat/v3.3/twilio-chat.min.js*/
        function chatInit(twilioChat){ 

            $scope.twilioChat = twilioChat;
            $scope.twilioChat.Client.create($scope.TOKEN).then(client => {
              console.log('Created chat client');
                $scope.chatClient = client;
                $scope.chatClient.getSubscribedChannels().then(function(){
                    $scope.chatReady = true;
                    console.log('chat is ready');
                    createOrJoinMonitorChannel();
                });
                
                
            }).catch((err) =>{
                console.error(err);
                
            })
        }
    
    function createOrJoinMonitorChannel(){
            
            $scope.chatClient.getChannelByUniqueName($scope.monitor_listen)
                .then(function(channel){
                      $scope.monitorListenChannel = channel;
                      setupMonitorChannel();
                      }).catch(function(err){
                console.log(err);
                $scope.chatClient.createChannel({
                    uniqueName: $scope.monitor_listen,
                    friendlyName: $scope.monitor_listen,
                    
                }).then(function(channel){
                    $scope.monitorListenChannel = channel;
                   setupMonitorChannel();
                }).catch(function(err){
                    console.log('Monitor Channel could not be created');
                    console.log(err);
                });
            });
            
        }
    
    function setupMonitorChannel(){
            var status = $scope.monitorListenChannel.state.status;
            if(status !== 'joined'){
            $scope.monitorListenChannel.join().then(function(channel){
                
                });
            }else{
            
            }
            $scope.monitorListenChannel.on('memberJoined',function(m){
                console.log('member joined');
                
            });
            $scope.monitorListenChannel.on('memberLeft',function(m){
                console.log('member left');
                
            });
                if($scope.chatClient.reachabilityEnabled){
                    console.log('Enabled');
                }else{
                    console.log('Not Enabled');
                }
                
            
              $member_promise = $scope.monitorListenChannel.getMembers();
              $member_promise.then(members=>{
                 console.log(members); 
                 members.forEach(member=>{
                     member.on('userInfoUpdated',function(user){
                        console.log('user info updated') ;
                     });
                 })
              });
            
            $scope.monitorListenChannel.on('messageAdded', function(message){
                var data = isJSONSTR(message.body);
                $handler.classroomMonitor(data);
            });
        }
1 Answers

When you enable Reachability it fires off an event when a user is online or not. You can use the user.on("updated", ({user, updateReasons}) => {}) event listener. Depending on which version you are using, userInfoUpdated might not be supported.

When you are listening for when a member has left a channel, you have to call channel.leave() yourself. Does this help?

https://media.twiliocdn.com/sdk/js/chat/releases/4.0.0/docs/User.html

Related