Building Real-Time Chat with React and SignalR
Learn how to build a dynamic chat application using React, SignalR, and Markdown streaming. This guide covers setting up the environment, integrating real-time messaging, and rendering Markdown content.
AI & Machine Learning Series — 25 articles
- Using ChatGPT for C# Development
- Trivia Spark: Building a Trivia App with ChatGPT
- Creating a Key Press Counter with Chat GPT
- Using Large Language Models to Generate Structured Data
- Prompt Spark: Revolutionizing LLM System Prompt Management
- Integrating Chat Completion into Prompt Spark
- WebSpark: Transforming Web Project Mechanics
- Accelerate Azure DevOps Wiki Writing
- The Brain Behind JShow Trivia Demo
- Building My First React Site Using Vite
- Adding Weather Component: A TypeScript Learning Journey
- Interactive Chat in PromptSpark With SignalR
- Building Real-Time Chat with React and SignalR
- Workflow-Driven Chat Applications Powered by Adaptive Cards
- Creating a Law & Order Episode Generator
- The Transformative Power of MCP
- The Impact of Input Case on LLM Categorization
- The New Era of Individual Agency: How AI Tools Empower Self-Starters
- AI Observability Is No Joke
- ChatGPT Meets Jeopardy: C# Solution for Trivia Aficionados
- Mastering LLM Prompt Engineering
- English: The New Programming Language of Choice
- Mountains of Misunderstanding: The AI Confidence Trap
- Measuring AI's Contribution to Code
- Building MuseumSpark - Why Context Matters More Than the Latest LLM
Building Real-Time Chat with React, SignalR, and Markdown Streaming
Building Real-Time Chat with React and SignalR
Introduction
Real-time chat looks simple from the outside — messages appear, characters stream in, the page stays smooth. The interesting work sits underneath: a React front end coordinating with SignalR for live messaging and a Markdown streaming pipeline that has to render incomplete content without flicker. Worth examining how the pieces fit together.
Prerequisites
Before we begin, ensure you have the following tools and knowledge:
- Node.js and npm: Make sure you have Node.js and npm installed on your machine.
- Basic knowledge of React: Familiarity with React components and hooks.
- Understanding of TypeScript: Basic understanding of TypeScript is recommended.
- SignalR basics: Some experience with SignalR will be helpful.
Setting Up the React Application
-
Create a new React app:
npx create-react-app real-time-chat --template typescript -
Install necessary packages:
npm install @microsoft/signalr marked
Integrating SignalR for Real-Time Messaging
SignalR is a library that simplifies adding real-time web functionality to applications. It allows server-side code to push content to clients instantly.
Setting Up SignalR Client
-
Create a SignalR connection:
import * as signalR from "@microsoft/signalr"; const connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").configureLogging(signalR.LogLevel.Information).build(); -
Start the connection:
connection.start().catch((err) => console.error(err.toString())); -
Handle incoming messages:
connection.on("ReceiveMessage", (user, message) => { const msg = document.createElement("div"); msg.textContent = `${user}: ${message}`; document.getElementById("messagesList").appendChild(msg); });
Implementing Markdown Streaming
Markdown allows users to format text using simple syntax. We will use the marked library to parse and render Markdown.
-
Render Markdown messages:
import { marked } from "marked"; function renderMarkdown(message: string): string { return marked(message); } -
Display Markdown in the chat:
const markdownMessage = renderMarkdown(message); document.getElementById("messagesList").innerHTML += `<div>${markdownMessage}</div>`;
Conclusion
By following these steps, you have successfully created a real-time chat application using React, SignalR, and Markdown streaming. This application allows users to send and receive messages in real-time while rendering Markdown content dynamically.
Additional Resources
Next Steps
Consider adding more features such as user authentication, message history, and emoji support to enhance your chat application.


