When i call any method. user get logout by itself

When i call any method i get logout why is that happening , even i have included login method at top.

Here my code:
function send_mesage()
{
var appSetting = new CometChat.AppSettingsBuilder().subscribePresenceForAllUsers().setRegion(region).build();
CometChat.init(appid, appSetting).then(
() => {
//console.log(“Initialization completed successfully”);
var user = CometChat.getLoggedinUser().then(
user => {
console.log(“user details:”, { user });

			//paste the code here
			var messageText = document.getElementById("message").value;						
			var receiverID = document.getElementById("reciever_id").value;	

			
			var receiverType = CometChat.RECEIVER_TYPE.USER;
			var textMessage = new CometChat.TextMessage(
			  receiverID,
			  messageText,
			  receiverType
			);

			CometChat.sendMessage(textMessage).then(
			  message => {
				console.log("Message sent successfully:", message);
				var html = '';
				var myObj = '';
				var avatar = '';
				var msg_type = '';
				console.log('test1');
				avatar = 'https://ptetutorials.com/images/user-profile.png';
				if(message.receiverId==comet_uid) {
					msg_type = 'sent';
				}
				else {
					msg_type = 'replies';
				}
				console.log('test2');
				html += '<li class="'+msg_type+'"><img src="'+avatar+'" alt="" /><p>' + message.data.text + '</p></li>';
				console.log(html);
				document.getElementById("message_list").innerHTML = document.getElementById("message_list").innerHTML + html;
				//document.getElementById("message").value='';
			  },
			  error => {
				console.log("Message sending failed with error:", error);
			  }
			);
		  },
		  error => {
			console.log("error getting details:", { error });
		  }
		);
			
		  },
		  error => {
			console.log("Initialization failed with error:", error);
			// Check the reason for error and take appropriate action.
		  }
		);
	}
1 Like

Hello @Priyanka09,

Can you please elaborate a little more on what issue you are facing exactly. Also can you please let us know in which framework you are integrating CometChat SDK. You should not initialize and login inside every function call. It should be done only once when the page loads. Also, you do not need to call the login function every time. The SDK internally stores the logged-in user. The flow should be like this:

  1. Page loads
  2. On page load, call CometChat.init()
  3. Check if any logged in user is present , by calling CometChat.getLoggedinUser().
  4. If the CometChat.getLoggedinUser() returns null, then you need to call CometChat.login(), else you can use the SDK as the user is already logged in.
1 Like

Thanks Mayur. I am using javascript SDK
when i call method directly without init and login, it does not worked ofr me, i called only on page loads . What can i do in that case, i did the same what you mentioned above but it did not worked for me, can you please call me my skype id is virtualemployee572@outlook.com.

Please do revert me, i am stucked

I called this method for sending message

function search_users(val) {

				var limit = 30;
					var usersRequest = new CometChat.UsersRequestBuilder().setLimit(limit).setSearchKeyword(val).build();

					usersRequest.fetchNext().then(
					  userList => {
						/* userList will be the list of User class. */
						console.log("User list received:", userList);
						/* retrived list can be used to display contact list. */
						var avatar = '';
						var html = '';
						var login_status = '';
						for (i = 0; i < userList.length; i++)  {
							console.log("1:", userList[i].uid);
							if(comet_uid!=userList[i].uid) {
								
								if((userList[i].status=='offline')) {
									login_status = 'busy';
								}
								else {
									login_status = 'online';
								}
								avatar = 'https://ptetutorials.com/images/user-profile.png';
								
								html += `<li class="contact" onclick="get_chat('`+userList[i].uid+`')">
										<div class="wrap">
											<span class="contact-status `+login_status+`"></span>
											<img src="`+avatar+`" alt="`+userList[i].name+`">
											<div class="meta">
												<p class="name">`+userList[i].name+`</p>
												
											</div>
										</div>
									</li>`;
							}
						}
						//console.log(html);
						document.getElementById("user_list").innerHTML = html;
						
					  },
					  error => {
						console.log("User list fetching failed with error:", error);
					  }
					);			
			  
				
			  			
		}

It did not worked for me

Hello @Priyanka09,

The code you have shared above is to fetch users and not for sending messages. I am confused so as to what issue are you facing exactly. Please share the relevant code snippet so that we can help you solve your issue.

Sorry i shared wrong method by mistake

this the code i am using to send messages

//paste the code here
var messageText = document.getElementById(“message”).value;
var receiverID = document.getElementById(“reciever_id”).value;

			var receiverType = CometChat.RECEIVER_TYPE.USER;
			var textMessage = new CometChat.TextMessage(
			  receiverID,
			  messageText,
			  receiverType
			);

			CometChat.sendMessage(textMessage).then(
			  message => {
				console.log("Message sent successfully:", message);
				var html = '';
				var myObj = '';
				var avatar = '';
				var msg_type = '';
				var d = new Date((parseInt(message.sentAt)*1000));
				var months = [
				  'January',
				  'February',
				  'March',
				  'April',
				  'May',
				  'June',
				  'July',
				  'August',
				  'September',
				  'October',
				  'November',
				  'December'
				];	
				
				
				var year = d.getFullYear();
				var monthIndex = d.getMonth();
				var date = d.getDate()+1;
				var monthName = months[monthIndex];
				
				var formatted = date+' '+monthName+', '+year;
				
				avatar = 'https://ptetutorials.com/images/user-profile.png';
				if(message.receiverId==comet_uid) {
					msg_type = 'sent';
				}
				else {
					msg_type = 'replies';
				}
				
				html += '<li class="'+msg_type+'"><img src="'+avatar+'" alt="" /><p>' + message.data.text + '<span class="chat_date" >'+formatted+'</span></p></li>';
				console.log(html);
				document.getElementById("message_list").innerHTML = document.getElementById("message_list").innerHTML + html;
				//document.getElementById("message").value='';
			  },
			  error => {
				console.log("Message sending failed with error:", error);
			  }
			);

I mean the same , it is not working alone, without calling init and login,Either it is fetching users or sending message.

Here is the link where i am integrating https://www.booksbase.com/chat/index#

Hello @Priyanka09,

The code snippet seems fine but can you please let us know the flow of your app and the exact issue you are facing. Also, it would be helpful if you could share the steps to recreate the issue. We do not know on what tech stack you are trying to integrate the CometChat Pro SDK. Kindly, let us know if you are using React or Angular or any other framework. If you want to know how to integrate our JS SDK, please refer to our ReactJS sample app. I am sharing the link to our sample app below.