Blazor Chat Component Overview
The Telerik UI for Blazor Chat component enables developers to build modern conversational interfaces in Blazor applications. It supports rich messaging, AI integrations, custom templates, file uploads, and accessibility features. The component is designed for extensibility, allowing integration with chatbots, LLMs, and custom business logic.
Creating Blazor Chat
- Add the
<TelerikChat>tag to your page. - Set the
Dataparameter to a collection of messages. - Set the
AuthorIdparameter to identify the current user. - Subscribe to the
OnSendMessageevent to handle new messages. - (optional) Configure additional settings like dimensions and speech-to-text support.
- (optional) Define the Chat model property names if they don't match the defaults.
Basic configuration of the Telerik Chat
<TelerikChat Data="@ChatData"
AuthorId="@CurrentUserId"
EnableSpeechToText="@ChatSpeechToTextEnabled"
OnSendMessage="@OnChatSendMessage"
Height="90vh"
Width="400px">
</TelerikChat>
@code {
private List<Message> ChatData { get; set; } = new();
private string CurrentUserId { get; set; } = "user1";
private bool ChatSpeechToTextEnabled { get; set; } = true;
private async Task OnChatSendMessage(ChatSendMessageEventArgs args)
{
var newMessage = new Message
{
AuthorId = CurrentUserId,
Text = args.Message
};
ChatData.Add(newMessage);
}
protected override void OnInitialized()
{
ChatData = new List<Message>();
for (int i = 1; i <= 2; i++)
{
ChatData.Add(new Message
{
Text = i == 1 ? "Hello! How can I help you today?" : "Hi there! I'm looking for information about the new features.",
AuthorId = i == 1 ? "assistant" : "user1",
Timestamp = DateTime.Now.AddMinutes(-5 + i)
});
}
}
public class Message
{
public string Id { get; set; } = Guid.NewGuid().ToString();
public string AuthorId { get; set; } = string.Empty;
public string AuthorName { get; set; } = string.Empty;
public string AuthorImageUrl { get; set; } = string.Empty;
public IEnumerable<FileSelectFileInfo> Files { get; set; } = new List<FileSelectFileInfo>();
public bool IsDeleted { get; set; }
public bool IsPinned { get; set; }
public bool IsTyping { get; set; }
public string ReplyToId { get; set; } = string.Empty;
public string Status { get; set; } = string.Empty;
public List<string> SuggestedActions { get; set; } = new();
public string Text { get; set; } = string.Empty;
public DateTime Timestamp { get; set; } = DateTime.Now;
}
}
Data Binding
The Chat component supports binding to collections of messages and user data. The component provides flexible field mapping to accommodate different data models. Read more about Chat data binding...
Messages
The Chat component offers rich messaging capabilities including context menus, toolbars, appearance customization, and persistence features. Messages can include text, files, and custom content. Read more about Chat messages...
Templates and Customization
The Chat component provides extensive template support for customizing the appearance and behavior of messages, timestamps, suggestions, message box, and header. Read more about Chat templates...
Integrations
Connect the Chat component with AI services, chatbots, and external APIs to create intelligent conversational experiences. The component supports integration with popular AI services and custom bot frameworks. Read more about Chat integrations...
File Uploads and Media
Enable file uploads and media sharing in your chat application. The component supports configurable file upload settings and speech-to-text functionality for enhanced user experience. Read more about file uploads and media...
Events
The Chat component exposes various events that allow you to implement custom functionality and handle user interactions. Key events include message sending, file uploads, suggestion clicks, and message actions. Read more about Chat events...
Chat Parameters
The Chat component provides a variety of parameters:
| Parameter | Type and Default Value | Description |
|---|---|---|
AttachmentsField | string ( "Attachments") | The name of the field containing message file attachments. |
AuthorId | string | The ID of the current user sending messages. |
AuthorIdField | string ( "AuthorId") | The name of the field containing the author identifier. |
AuthorImageUrlField | string ( "AuthorImageUrl") | The name of the field containing the author's avatar image URL. |
AuthorNameField | string ( "AuthorName") | The name of the field containing the author display name. |
Data | IEnumerable<TItem> | The data source for chat messages. |
EnableFileUpload | bool ( false) | Enables file upload functionality in the chat input. |
EnableSpeechToText | bool ( true) | Enables speech-to-text functionality with microphone input. |
Height | string | The height of the chat component in CSS units. |
IsTypingField | string | The name of the field that indicates whether a message represents a typing indicator. |
MessageFilesLayoutMode | ChatMessageFilesLayoutMode enum ( Vertical) | Controls how file attachments are displayed (Vertical, Horizontal, or Wrap). |
MessageWidthMode | MessageWidthMode enum ( Standard) | Controls the width behavior of messages (Standard or Full). |
SuggestedActionsLayoutMode | ChatSuggestedActionsLayoutMode enum ( Wrap) | Controls how suggested actions are displayed (Wrap, Scroll, or ScrollButtons). |
Suggestions | IEnumerable<string> | Collection of quick reply suggestions. |
SuggestionsLayoutMode | ChatSuggestionsLayoutMode enum ( Wrap) | Controls how message suggestions are displayed (Wrap, Scroll, or ScrollButtons). |
TextField | string ( "Text") | The name of the field containing the message content. |
TimestampField | string ( "Timestamp") | The name of the field containing the message timestamp. |
Width | string | The width of the chat component in CSS units. |
Chat Reference and Methods
The Chat component exposes a Refresh() method that refreshes the component and scrolls to the latest messages. To execute the method, obtain a reference to the Chat instance via @ref.
How to obtain a Chat reference and call methods
<TelerikChat @ref="@ChatRef"
Data="@ChatData"
OnSendMessage="@OnChatSendMessage">
</TelerikChat>
<TelerikButton OnClick="@OnRefreshChatClick">Refresh Chat</TelerikButton>
@code {
private TelerikChat<ChatMessage>? ChatRef { get; set; }
private void OnRefreshChatClick()
{
ChatRef?.Refresh();
}
}
Next Steps
- Get started with Chat data binding
- Customize Chat templates
- Handle Chat events
- Configure file uploads and media