Skip to content

Conversation

@deepak0x
Copy link
Contributor

@deepak0x deepak0x commented Jan 1, 2026

Enable Channel Switching by Channel Name

Closes #452

Overview

This PR adds the ability to switch channels using channel names instead of requiring room IDs. Previously, users had to find and provide the room ID to switch channels, which was difficult. Now, users can simply provide the channel name (e.g., "public", "embedded", "general") and the component will automatically resolve it to the correct room ID.

Problem

Before this change, the EmbeddedChat component only supported switching channels using roomId. Channel IDs are not user-friendly and difficult to find, making it hard for developers to integrate the component with specific channels.

Solution

The component now supports channel switching using the channelName prop. When channelName is provided:

  1. The component waits for user authentication
  2. Once authenticated, it calls the Rocket.Chat API to resolve the channel name to a room ID
  3. The resolved room ID is used to connect to the correct channel
  4. Messages and channel information are properly loaded for the resolved channel

Changes

Core Functionality

  • Added resolvedRoomId state to manage dynamically resolved room IDs
  • Added explicitRoomId state to track when a room ID is explicitly provided
  • Implemented channel name to room ID resolution using /api/v1/rooms.info?roomName=... API endpoint
  • Updated re-instantiation logic to properly clear messages when switching channels
  • Added proper error handling for authentication and API failures

UI Updates

  • Updated ChatHeader to prioritize channelName prop for display
  • Updated ChatInput placeholder to use channelName when available
  • Added error handling for unauthorized channel info requests

API Changes

  • Updated EmbeddedChatApi.channelInfo() to check for authentication before making API calls
  • Returns proper error response when authentication is not available

Usage

Using Channel Name (New)

<EmbeddedChat
  host="http://localhost:3000"
  channelName="public"
  auth={{
    flow: 'PASSWORD'
  }}
/>

Using Room ID (Still Supported)

<EmbeddedChat
  host="http://localhost:3000"
  roomId="6956b4ced42fcf35f5783b1d"
  channelName="public"
  auth={{
    flow: 'PASSWORD'
  }}
/>

How It Works

  1. If roomId is explicitly provided, it is used directly
  2. If only channelName is provided:
    • Component waits for user authentication
    • Once authenticated, makes API call to resolve channel name to room ID
    • Uses resolved room ID to connect to the channel
  3. If neither is provided, defaults to "GENERAL" channel

Before / After

Before
before
After (with channel name)
after channel name
After (with room ID)
after room id

Technical Details

  • The resolution happens asynchronously after authentication
  • The component properly handles cases where authentication is not yet complete
  • Messages and channel state are cleared when switching channels to prevent showing wrong channel data
  • The component re-instantiates the connection when the resolved room ID changes

Testing

  • Tested with channel names: "public", "embedded", "general"
  • Verified that channel switching works correctly with only channel name provided
  • Verified that explicit room ID still works as before
  • Verified that messages are properly cleared when switching channels
  • Verified that authentication errors are handled gracefully

- Add channelName to roomId resolution using Rocket.Chat API
- Implement resolvedRoomId state management to handle dynamic room resolution
- Fix re-instantiation logic to properly clear messages when switching channels
- Wait for authentication before resolving channelName to prevent 401 errors
- Update ChatHeader and ChatInput to prioritize channelName prop for display

Fixes: Channel switching now works correctly when only channelName is provided

const EmbeddedChat = (props) => {
const [config, setConfig] = useState(() => props);
// Track if roomId was explicitly provided (not just the default)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove all the comments

@deepak0x
Copy link
Contributor Author

deepak0x commented Jan 3, 2026

@Spiral-Memory I removed all the comments...

@deepak0x
Copy link
Contributor Author

deepak0x commented Jan 3, 2026

@Spiral-Memory I fixed that yarn lint issue
was coming due to empty block

@deepak0x
Copy link
Contributor Author

deepak0x commented Jan 3, 2026

@Spiral-Memory fixed the EmbeddedChatApi.ts to pass format check

@deepak0x deepak0x requested a review from Spiral-Memory January 8, 2026 21:30
if (data?.success && data?.room?._id) {
setResolvedRoomId(data.room._id);
} else {
setResolvedRoomId('GENERAL');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to have this fallback ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I set this If a user provides a channelName that doesn't exist or they lack access, falling back to 'GENERAL' hides the failure.

@Spiral-Memory
Copy link
Collaborator

Hi @deepak0x
This approach seems to involve too much manual work, so I wouldn’t recommend it.

async getRoomIdByName(channelName: string): Promise<string> { could be part of the API instead.

Rather than manually handling hasMounted, we could use a key-based approach. A custom hook, like useRoomId or something similar, could also be created.

Please look into finding a more elegant and efficient solution.

@Spiral-Memory Spiral-Memory added the nudge minor tweaks suggested label Jan 15, 2026
@deepak0x
Copy link
Contributor Author

Sure @Spiral-Memory

deepak0x and others added 5 commits January 15, 2026 15:17
- Add getRoomIdByName method to EmbeddedChatApi for resolving channel names to room IDs
- Create useRoomId custom hook to encapsulate room ID resolution logic
- Refactor EmbeddedChat to use key-based approach instead of manual state tracking
- Remove fallback to GENERAL when channel name resolution fails
- Display clear error messages when channel not found or inaccessible
- Show error UI and toast notifications for failed channel resolution
- Maintain backward compatibility with explicit roomId prop
- Use temporary 'GENERAL' roomId when channelName is provided but user is not authenticated yet
- Only show error UI when there's an actual error, not when waiting for auth
- Fixes e2e tests that were failing due to component not rendering
@deepak0x
Copy link
Contributor Author

Hey @Spiral-Memory
Have addressed issues reported by you.. pls check...

- Use roomId prop as fallback when resolvedRoomId is not available
- Ensure component renders when explicit roomId is provided in e2e tests
- Fixes e2e test failures where component was not rendering
@deepak0x deepak0x force-pushed the fix/embedded-chat-channelname-not-switching branch from a95c222 to aaf85c4 Compare January 18, 2026 13:01
- Remove toast dispatch usage before ToastBarProvider is mounted
- Prevent render crash that hid the embedded chat in e2e tests
- Add null check before creating RCInstance to prevent errors
- Ensure component always renders when roomId is provided
- Fixes e2e test failures where component was not visible
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

nudge minor tweaks suggested

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Change current channel with Channel Name

2 participants