Hi Chinmaya,
I am having the same or similar problems using the JavaScript SDK in a React app. The two issues are, when I try to create a new channel, the requestedUrl is different than the receivedUrl, and I’m also seeing the same as System_Yumi, the members array returned by SendBird for new and existing channels only ever has one member. Below is my code and the channelParams I’m sending, along with the response from the SDK. Any assistance would be greatly appreciated.
const handleCreateChat = async () => {
if (selectedContacts.length !== 1 || !user) {
console.error(‘Cannot create chat: exactly one contact must be selected’);
return;
}
try {
const selectedContact = selectedContacts[0];
console.log('Creating 1-on-1 chat with contact:', selectedContact);
// Create channel URL using both user IDs
const userIds = [user.chatUserId, selectedContact.userId];
console.log('User IDs:', userIds);
const channelUrl = `chat_${userIds.join('_')}`;
console.log('Attempting to create/get channel with URL:', channelUrl);
let groupChannel;
try {
groupChannel = await sb.current.groupChannel.getChannel(channelUrl);
console.log('Found existing channel:', groupChannel);
} catch (error) {
console.log('Error retrieving channel, attempting to create a new one:', error);
// Create channel params specifically for 1-on-1 chat
const channelParams: GroupChannelCreateParams = {
invitedUserIds: userIds,
name: selectedContact.nickname,
channelUrl: channelUrl,
isDistinct: true,
customType: 'one_to_one'
};
console.log('Channel params before creation:', {
channelParams
});
groupChannel = await sb.current.groupChannel.createChannel(channelParams);
console.log('SendBird response details:', {
requestedUrl: channelParams.channelUrl,
receivedUrl: groupChannel.url,
isDistinct: groupChannel.isDistinct,
customType: groupChannel.customType,
members: groupChannel.members.map((m: Member) => m.userId),
invitedUsers: groupChannel.invitedUsers
});
console.log('New channel created:', groupChannel);
}
// Verify the channel exists
if (!groupChannel) {
throw new Error('Failed to create or get channel');
}
// Create conversation object
const newConversation: ActiveConversation = {
channelUrl: groupChannel.url,
name: selectedContact.nickname,
members: [{
userId: selectedContact.userId,
nickname: selectedContact.nickname,
profileUrl: selectedContact.profileUrl || '/unverified.png'
}],
lastMessage: groupChannel.lastMessage?.message || '',
timestamp: groupChannel.lastMessage?.createdAt || groupChannel.createdAt,
unreadMessageCount: groupChannel.unreadMessageCount,
profileUrl: selectedContact.profileUrl || '/unverified.png',
otherUser: selectedContact,
customType: 'one_to_one'
};
console.log('Created conversation object:', newConversation);
// Update state using a callback to ensure we have the latest state
setActiveConversations(prevConversations => {
console.log('Previous conversations:', prevConversations);
// Check if conversation already exists
const existingIndex = prevConversations.findIndex(
conv => conv.channelUrl === groupChannel.url
);
if (existingIndex !== -1) {
console.log('Conversation exists at index:', existingIndex);
// If it exists, update it
const updatedConversations = [...prevConversations];
updatedConversations[existingIndex] = newConversation;
console.log('Updated conversations:', updatedConversations);
return updatedConversations;
} else {
// If it doesn't exist, add it to the beginning
console.log('Adding new conversation');
const updatedConversations = [newConversation, ...prevConversations];
console.log('Updated conversations:', updatedConversations);
return updatedConversations;
}
});
// Close the modal
handleCloseModal();
console.log('About to refresh channels, confirmed channel URL:', groupChannel.url);
// Force a refresh of the channels list
await fetchUserChannels();
// Select the new channel
console.log('Selecting new channel:', groupChannel.url);
await handleChannelSelect(groupChannel.url);
} catch (error) {
console.error('Error in handleCreateChat:', error);
if (error instanceof Error) {
console.error('Error details:', {
message: error.message,
stack: error.stack,
name: error.name
});
}
}
};
{
“channelParams”: {
“invitedUserIds”: [
“51bb9c1b-51b6-4a73-92e2-f760c8671206”,
“c7fdd974-2068-4337-a7c6-0d75ee825984”
],
“name”: “Mike Hudson”,
“channelUrl”: “chat_51bb9c1b-51b6-4a73-92e2-f760c8671206_c7fdd974-2068-4337-a7c6-0d75ee825984”,
“isDistinct”: true,
“customType”: “one_to_one”
}
}
{
“requestedUrl”: “chat_51bb9c1b-51b6-4a73-92e2-f760c8671206_c7fdd974-2068-4337-a7c6-0d75ee825984”,
“receivedUrl”: “chat_51bb9c1b-51b6-4a73-92e2-f760c8671206_84a12040-8e79-4e8d-b38f-3e3176b7644b”,
“isDistinct”: true,
“customType”: “one_to_one”,
“members”: [
“51bb9c1b-51b6-4a73-92e2-f760c8671206”
]
}